发布于 2015-09-14 15:11:02 | 165 次阅读 | 评论: 0 | 来源: 网络整理
This is a non-exhaustive list of helper functions for use in the C++ stream syntax. An exhaustive list is here: bsonmisc.h
Typical example of stream syntax:
BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
The server will add an _id automatically if it is not included explicitly.
BSONObj p = BSON( GENOID << "name" << "Joe" << "age" << 33 );
// result is: { _id : ..., name : "Joe", age : 33 }
less than, greater than, etc.
BSONObj p = BSON( "age" << GT << 21 );
// result is: { age : { $gt : 21 } }
Translates to current date
BSONObj p = BSON( "created" << DATENOW );
// result is: { created : "2009-10-09 11:41:42" }
MINKEY, MAXKEY
BSONObj p = BSON( "a" << MINKEY); // result is: { "a" : { "$minKey" : 1 } }OR
OR(BSON("x" << GT << 7), BSON("y" << LT << 6)) // result is: {$or: [{x: {$gt: 7}}, {y: {$lt: 6}}]}
Translates to null value (will appear in MongoDB 2.1)
BSONObj p = BSON( "name" << "Methuselah" << "age" << BSONNULL );
// result is: { name : "Methuselah", age : null }