本文整理汇总了C++中MutableDocument::addField方法的典型用法代码示例。如果您正苦于以下问题:C++ MutableDocument::addField方法的具体用法?C++ MutableDocument::addField怎么用?C++ MutableDocument::addField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MutableDocument
的用法示例。
在下文中一共展示了MutableDocument::addField方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: serialize
Document ExclusionNode::serialize() const {
MutableDocument output;
for (auto&& excludedField : _excludedFields) {
output.addField(excludedField, Value(false));
}
for (auto&& childPair : _children) {
output.addField(childPair.first, Value(childPair.second->serialize()));
}
return output.freeze();
}
示例2: run
void run() {
MutableDocument md;
md.addField("foo", Value(1));
ASSERT_EQUALS(1U, md.peek().size());
ASSERT_EQUALS(1, md.peek()["foo"].getInt());
md.addField("bar", Value(99));
ASSERT_EQUALS(2U, md.peek().size());
ASSERT_EQUALS(99, md.peek()["bar"].getInt());
// No assertion is triggered by a duplicate field name.
md.addField("a", Value(5));
Document final = md.freeze();
ASSERT_EQUALS(3U, final.size());
assertRoundTrips(final);
}
示例3: getValue
Value AccumulatorAvg::getValue() const {
if (!pCtx->getInShard()) {
double avg = 0;
if (count)
avg = doubleTotal / static_cast<double>(count);
return Value::createDouble(avg);
}
MutableDocument out;
out.addField(subTotalName, Value::createDouble(doubleTotal));
out.addField(countName, Value::createLong(count));
return Value::createDocument(out.freeze());
}
示例4: getValue
Value AccumulatorAvg::getValue(bool toBeMerged) const {
if (!toBeMerged) {
double avg = 0;
if (count)
avg = doubleTotal / static_cast<double>(count);
return Value(avg);
}
else {
MutableDocument out;
out.addField(subTotalName, Value(doubleTotal));
out.addField(countName, Value(count));
return Value(out.freeze());
}
}
示例5: fields
boost::optional<Document> DocumentSourceRedact::redactObject() {
const Value expressionResult = _expression->evaluate(_variables.get());
if (expressionResult == keepVal) {
return _variables->getDocument(_currentId);
} else if (expressionResult == pruneVal) {
return boost::optional<Document>();
} else if (expressionResult == descendVal) {
const Document in = _variables->getDocument(_currentId);
MutableDocument out;
out.copyMetaDataFrom(in);
FieldIterator fields(in);
while (fields.more()) {
const Document::FieldPair field(fields.next());
// This changes CURRENT so don't read from _variables after this
const Value val = redactValue(field.second);
if (!val.missing()) {
out.addField(field.first, val);
}
}
return out.freeze();
} else {
uasserted(17053,
str::stream() << "$redact's expression should not return anything "
<< "aside from the variables $$KEEP, $$DESCEND, and "
<< "$$PRUNE, but returned "
<< expressionResult.toString());
}
}
示例6: fromBsonWithMetaData
Document Document::fromBsonWithMetaData(const BSONObj& bson) {
MutableDocument md;
BSONObjIterator it(bson);
while (it.more()) {
BSONElement elem(it.next());
auto fieldName = elem.fieldNameStringData();
if (fieldName[0] == '$') {
if (fieldName == metaFieldTextScore) {
md.setTextScore(elem.Double());
continue;
} else if (fieldName == metaFieldRandVal) {
md.setRandMetaField(elem.Double());
continue;
} else if (fieldName == metaFieldSortKey) {
md.setSortKeyMetaField(elem.Obj());
continue;
}
}
// Note: this will not parse out metadata in embedded documents.
md.addField(fieldName, Value(elem));
}
return md.freeze();
}
示例7: if
boost::optional<Document> DocumentSourceRedact::redactObject(const Variables& in) {
const Value expressionResult = _expression->evaluate(in);
if (expressionResult == keepVal) {
return in.current.getDocument();
}
else if (expressionResult == pruneVal) {
return boost::optional<Document>();
}
else if (expressionResult == descendVal) {
MutableDocument out;
FieldIterator fields(in.current.getDocument());
while (fields.more()) {
const Document::FieldPair field(fields.next());
const Value val = redactValue(in, field.second);
if (!val.missing()) {
out.addField(field.first, val);
}
}
return out.freeze();
}
else {
uasserted(17053, str::stream() << "$redact's expression should not return anything "
<< "aside from the variables $$KEEP, $$DESCEND, and "
<< "$$PRUNE, but returned "
<< expressionResult.toString());
}
}
示例8: makeDocument
Document DocumentSourceGroup::makeDocument(const Value& id,
const Accumulators& accums,
bool mergeableOutput) {
const size_t n = vFieldName.size();
MutableDocument out (1 + n);
/* add the _id field */
out.addField("_id", id);
/* add the rest of the fields */
for(size_t i = 0; i < n; ++i) {
Value val = accums[i]->getValue(mergeableOutput);
if (val.missing()) {
// we return null in this case so return objects are predictable
out.addField(vFieldName[i], Value(BSONNULL));
}
else {
out.addField(vFieldName[i], val);
}
}
return out.freeze();
}
示例9: makeDocument
Document DocumentSourceGroup::makeDocument(
const GroupsType::iterator &rIter) {
vector<intrusive_ptr<Accumulator> > *pGroup = &rIter->second;
const size_t n = vFieldName.size();
MutableDocument out (1 + n);
/* add the _id field */
out.addField("_id", rIter->first);
/* add the rest of the fields */
for(size_t i = 0; i < n; ++i) {
Value pValue((*pGroup)[i]->getValue());
if (pValue.missing()) {
// we return undefined in this case so return objects are predictable
out.addField(vFieldName[i], Value(BSONUndefined));
}
else {
out.addField(vFieldName[i], pValue);
}
}
return out.freeze();
}
示例10: applyTransformation
//.........这里部分代码省略.........
// Extract the field names of $unset document.
vector<Value> removedFieldsVector;
if (removedFields.getType() == BSONType::Object) {
auto iter = removedFields.getDocument().fieldIterator();
while (iter.more()) {
removedFieldsVector.push_back(Value(iter.next().first));
}
}
updateDescription = Value(Document{
{"updatedFields", updatedFields.missing() ? Value(Document()) : updatedFields},
{"removedFields", removedFieldsVector}});
} else {
operationType = DocumentSourceChangeStream::kReplaceOpType;
fullDocument = input[repl::OplogEntry::kObjectFieldName];
}
documentKey = input[repl::OplogEntry::kObject2FieldName];
break;
}
case repl::OpTypeEnum::kCommand: {
if (!input.getNestedField("o.drop").missing()) {
operationType = DocumentSourceChangeStream::kDropCollectionOpType;
// The "o.drop" field will contain the actual collection name.
nss = NamespaceString(nss.db(), input.getNestedField("o.drop").getString());
} else if (!input.getNestedField("o.renameCollection").missing()) {
operationType = DocumentSourceChangeStream::kRenameCollectionOpType;
// The "o.renameCollection" field contains the namespace of the original collection.
nss = NamespaceString(input.getNestedField("o.renameCollection").getString());
// The "o.to" field contains the target namespace for the rename.
const auto renameTargetNss =
NamespaceString(input.getNestedField("o.to").getString());
doc.addField(DocumentSourceChangeStream::kRenameTargetNssField,
Value(Document{{"db", renameTargetNss.db()},
{"coll", renameTargetNss.coll()}}));
} else if (!input.getNestedField("o.dropDatabase").missing()) {
operationType = DocumentSourceChangeStream::kDropDatabaseOpType;
// Extract the database name from the namespace field and leave the collection name
// empty.
nss = NamespaceString(nss.db());
} else {
// All other commands will invalidate the stream.
operationType = DocumentSourceChangeStream::kInvalidateOpType;
}
// Make sure the result doesn't have a document key.
documentKey = Value();
break;
}
case repl::OpTypeEnum::kNoop: {
operationType = DocumentSourceChangeStream::kNewShardDetectedOpType;
// Generate a fake document Id for NewShardDetected operation so that we can resume
// after this operation.
documentKey = Value(Document{{DocumentSourceChangeStream::kIdField,
input[repl::OplogEntry::kObject2FieldName]}});
break;
}
default: { MONGO_UNREACHABLE; }
}
// UUID should always be present except for invalidate and dropDatabase entries.
if (operationType != DocumentSourceChangeStream::kInvalidateOpType &&
operationType != DocumentSourceChangeStream::kDropDatabaseOpType) {
invariant(!uuid.missing(), "Saw a CRUD op without a UUID");
示例11: getNext
DocumentSource::GetNextResult DocumentSourceCurrentOp::getNext() {
pExpCtx->checkForInterrupt();
if (_ops.empty()) {
_ops = pExpCtx->mongoProcessInterface->getCurrentOps(
pExpCtx->opCtx, _includeIdleConnections, _includeOpsFromAllUsers, _truncateOps);
_opsIter = _ops.begin();
if (pExpCtx->fromMongos) {
_shardName = pExpCtx->mongoProcessInterface->getShardName(pExpCtx->opCtx);
uassert(40465,
"Aggregation request specified 'fromMongos' but unable to retrieve shard name "
"for $currentOp pipeline stage.",
!_shardName.empty());
}
}
if (_opsIter != _ops.end()) {
if (!pExpCtx->fromMongos) {
return Document(*_opsIter++);
}
// This $currentOp is running in a sharded context.
invariant(!_shardName.empty());
const BSONObj& op = *_opsIter++;
MutableDocument doc;
// Add the shard name to the output document.
doc.addField(kShardFieldName, Value(_shardName));
// For operations on a shard, we change the opid from the raw numeric form to
// 'shardname:opid'. We also change the fieldname 'client' to 'client_s' to indicate
// that the IP is that of the mongos which initiated this request.
for (auto&& elt : op) {
StringData fieldName = elt.fieldNameStringData();
if (fieldName == kOpIdFieldName) {
uassert(ErrorCodes::TypeMismatch,
str::stream() << "expected numeric opid for $currentOp response from '"
<< _shardName
<< "' but got: "
<< typeName(elt.type()),
elt.isNumber());
std::string shardOpID = (str::stream() << _shardName << ":" << elt.numberInt());
doc.addField(kOpIdFieldName, Value(shardOpID));
} else if (fieldName == kClientFieldName) {
doc.addField(kMongosClientFieldName, Value(elt.str()));
} else {
doc.addField(fieldName, Value(elt));
}
}
return doc.freeze();
}
return GetNextResult::makeEOF();
}
示例12: nss
Document DocumentSourceChangeStream::Transformation::applyTransformation(const Document& input) {
MutableDocument doc;
// Extract the fields we need.
checkValueType(input[repl::OplogEntry::kOpTypeFieldName],
repl::OplogEntry::kOpTypeFieldName,
BSONType::String);
string op = input[repl::OplogEntry::kOpTypeFieldName].getString();
Value ts = input[repl::OplogEntry::kTimestampFieldName];
Value ns = input[repl::OplogEntry::kNamespaceFieldName];
checkValueType(ns, repl::OplogEntry::kNamespaceFieldName, BSONType::String);
NamespaceString nss(ns.getString());
Value id = input.getNestedField("o._id");
// Non-replace updates have the _id in field "o2".
Value documentId = id.missing() ? input.getNestedField("o2._id") : id;
StringData operationType;
Value fullDocument;
Value updateDescription;
// Deal with CRUD operations and commands.
auto opType = repl::OpType_parse(IDLParserErrorContext("ChangeStreamEntry.op"), op);
switch (opType) {
case repl::OpTypeEnum::kInsert: {
operationType = kInsertOpType;
fullDocument = input[repl::OplogEntry::kObjectFieldName];
break;
}
case repl::OpTypeEnum::kDelete: {
operationType = kDeleteOpType;
break;
}
case repl::OpTypeEnum::kUpdate: {
if (id.missing()) {
operationType = kUpdateOpType;
checkValueType(input[repl::OplogEntry::kObjectFieldName],
repl::OplogEntry::kObjectFieldName,
BSONType::Object);
Document opObject = input[repl::OplogEntry::kObjectFieldName].getDocument();
Value updatedFields = opObject["$set"];
Value removedFields = opObject["$unset"];
// Extract the field names of $unset document.
vector<Value> removedFieldsVector;
if (removedFields.getType() == BSONType::Object) {
auto iter = removedFields.getDocument().fieldIterator();
while (iter.more()) {
removedFieldsVector.push_back(Value(iter.next().first));
}
}
updateDescription = Value(Document{
{"updatedFields", updatedFields.missing() ? Value(Document()) : updatedFields},
{"removedFields", removedFieldsVector}});
} else {
operationType = kReplaceOpType;
fullDocument = input[repl::OplogEntry::kObjectFieldName];
}
break;
}
case repl::OpTypeEnum::kCommand: {
operationType = kInvalidateOpType;
// Make sure the result doesn't have a document id.
documentId = Value();
break;
}
default: { MONGO_UNREACHABLE; }
}
// Construct the result document.
Value documentKey;
if (!documentId.missing()) {
documentKey = Value(Document{{kIdField, documentId}});
}
// Note that 'documentKey' might be missing, in which case it will not appear in the output.
Document resumeToken{{kClusterTimeField, Document{{kTimestampField, ts}}},
{kNamespaceField, ns},
{kDocumentKeyField, documentKey}};
doc.addField(kIdField, Value(resumeToken));
doc.addField(kOperationTypeField, Value(operationType));
doc.addField(kFullDocumentField, fullDocument);
// "invalidate" entry has fewer fields.
if (opType == repl::OpTypeEnum::kCommand) {
return doc.freeze();
}
doc.addField(kNamespaceField, Value(Document{{"db", nss.db()}, {"coll", nss.coll()}}));
doc.addField(kDocumentKeyField, documentKey);
// Note that 'updateDescription' might be the 'missing' value, in which case it will not be
// serialized.
doc.addField("updateDescription", updateDescription);
return doc.freeze();
}