本文整理汇总了C++中mongo::BSONObj::hasElement方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::hasElement方法的具体用法?C++ BSONObj::hasElement怎么用?C++ BSONObj::hasElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongo::BSONObj
的用法示例。
在下文中一共展示了BSONObj::hasElement方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getField
/* ****************************************************************************
*
* ContextElementResponse::ContextElementResponse -
*
* This constructor builds the CER object based in a BSON object taken from the
* entities collection at DB.
*
* Note that statusCode is not touched by this constructor.
*/
ContextElementResponse::ContextElementResponse(const mongo::BSONObj& entityDoc, const AttributeList& attrL, bool includeEmpty)
{
prune = false;
// Entity
BSONObj id = getField(entityDoc, "_id").embeddedObject();
contextElement.entityId.fill(getStringField(id, ENT_ENTITY_ID), getStringField(id, ENT_ENTITY_TYPE), "false");
contextElement.entityId.servicePath = id.hasField(ENT_SERVICE_PATH) ? getStringField(id, ENT_SERVICE_PATH) : "";
/* Get the location attribute (if it exists) */
std::string locAttr;
if (entityDoc.hasElement(ENT_LOCATION))
{
locAttr = getStringField(getObjectField(entityDoc, ENT_LOCATION), ENT_LOCATION_ATTRNAME);
}
//
// Attribute vector
// FIXME P5: constructor for BSONObj could be added to ContextAttributeVector/ContextAttribute classes, to make building more modular
//
BSONObj attrs = getField(entityDoc, ENT_ATTRS).embeddedObject();
std::set<std::string> attrNames;
attrs.getFieldNames(attrNames);
for (std::set<std::string>::iterator i = attrNames.begin(); i != attrNames.end(); ++i)
{
std::string attrName = *i;
BSONObj attr = getField(attrs, attrName).embeddedObject();
ContextAttribute* caP = NULL;
ContextAttribute ca;
// Name and type
ca.name = dbDotDecode(basePart(attrName));
std::string mdId = idPart(attrName);
ca.type = getStringField(attr, ENT_ATTRS_TYPE);
// Skip attribute if the attribute is in the list (or attrL is empty)
if (!includedAttribute(ca, attrL))
{
continue;
}
/* It could happen (although very rarely) that the value field is missing in the
* DB for the attribute. The following is a safety check measure to protect against that */
if (!attr.hasField(ENT_ATTRS_VALUE))
{
caP = new ContextAttribute(ca.name, ca.type, "");
}
else
{
switch(getField(attr, ENT_ATTRS_VALUE).type())
{
case String:
ca.stringValue = getStringField(attr, ENT_ATTRS_VALUE);
if (!includeEmpty && ca.stringValue.length() == 0)
{
continue;
}
caP = new ContextAttribute(ca.name, ca.type, ca.stringValue);
break;
case NumberDouble:
ca.numberValue = getField(attr, ENT_ATTRS_VALUE).Number();
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;
case NumberInt:
ca.numberValue = (double) getIntField(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;
case Bool:
ca.boolValue = getBoolField(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.boolValue);
break;
case jstNULL:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->valueType = orion::ValueTypeNone;
break;
case Object:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->compoundValueP = new orion::CompoundValueNode(orion::ValueTypeObject);
compoundObjectResponse(caP->compoundValueP, getField(attr, ENT_ATTRS_VALUE));
break;
case Array:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->compoundValueP = new orion::CompoundValueNode(orion::ValueTypeVector);
//.........这里部分代码省略.........
示例2: getFieldF
/* ****************************************************************************
*
* ContextElementResponse::ContextElementResponse -
*
* This constructor builds the CER object based in a BSON object taken from the
* entities collection at DB.
*
* Note that statusCode is not touched by this constructor.
*/
ContextElementResponse::ContextElementResponse
(
const mongo::BSONObj& entityDoc,
const AttributeList& attrL,
bool includeEmpty,
const std::string& apiVersion
)
{
prune = false;
// Entity
BSONObj id = getFieldF(entityDoc, "_id").embeddedObject();
std::string entityId = getStringFieldF(id, ENT_ENTITY_ID);
std::string entityType = id.hasField(ENT_ENTITY_TYPE) ? getStringFieldF(id, ENT_ENTITY_TYPE) : "";
contextElement.entityId.fill(entityId, entityType, "false");
contextElement.entityId.servicePath = id.hasField(ENT_SERVICE_PATH) ? getStringFieldF(id, ENT_SERVICE_PATH) : "";
/* Get the location attribute (if it exists) */
std::string locAttr;
if (entityDoc.hasElement(ENT_LOCATION))
{
locAttr = getStringFieldF(getObjectFieldF(entityDoc, ENT_LOCATION), ENT_LOCATION_ATTRNAME);
}
//
// Attribute vector
// FIXME P5: constructor for BSONObj could be added to ContextAttributeVector/ContextAttribute classes, to make building more modular
//
BSONObj attrs = getObjectFieldF(entityDoc, ENT_ATTRS);
std::set<std::string> attrNames;
attrs.getFieldNames(attrNames);
for (std::set<std::string>::iterator i = attrNames.begin(); i != attrNames.end(); ++i)
{
std::string attrName = *i;
BSONObj attr = getObjectFieldF(attrs, attrName);
ContextAttribute* caP = NULL;
ContextAttribute ca;
// Name and type
ca.name = dbDotDecode(basePart(attrName));
std::string mdId = idPart(attrName);
ca.type = getStringFieldF(attr, ENT_ATTRS_TYPE);
// Skip attribute if the attribute is in the list (or attrL is empty or includes "*")
if (!includedAttribute(ca, attrL))
{
continue;
}
/* It could happen (although very rarely) that the value field is missing in the
* DB for the attribute. The following is a safety check measure to protect against that */
if (!attr.hasField(ENT_ATTRS_VALUE))
{
caP = new ContextAttribute(ca.name, ca.type, "");
}
else
{
switch(getFieldF(attr, ENT_ATTRS_VALUE).type())
{
case String:
ca.stringValue = getStringFieldF(attr, ENT_ATTRS_VALUE);
if (!includeEmpty && ca.stringValue.length() == 0)
{
continue;
}
caP = new ContextAttribute(ca.name, ca.type, ca.stringValue);
break;
case NumberDouble:
ca.numberValue = getNumberFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;
case NumberInt:
ca.numberValue = (double) getIntFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;
case Bool:
ca.boolValue = getBoolFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.boolValue);
break;
case jstNULL:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->valueType = orion::ValueTypeNone;
break;
//.........这里部分代码省略.........