from:http://blog.csdn.net/haha_mingg/article/details/8048422
MongoDB contains, in addition to these string, Integer, Boolean, double, NULL, array, and object basic data types, including: Date, object ID, binary data, regular E Xpression, and code these additional data types.
1. Timestamp type
The timestamp type is supported from version 1.8, and timestamp has a special usage: The field of the timestamp type must be in the first two bits of the document. See below for an example://In the third field > Db.coll.insert ({_id:1,x:2 , Y:new Timestamp ()}); > Db.coll.findOne ({_id:1}); {"_id": 1, "X": 2, "Y": {"T": 0, "I": 0}}//In the second field > Db.coll.insert ({_id:2,y:new Timestamp (), x:2}); > Db.coll.findOne ({_id:2}); {"_id": 2, "Y": {"T": 1306746538000, "I": 1}, "X": 2}
2. Objectid type
In MongoDB, almost every document (except for some system Collection or some capped Collection) requires a primary key: _ID, which uniquely identifies them, usually-its value is the Objectid type. When a user inserts a new record into the document, if the _id attribute is not specified, MongoDB automatically generates a value of type Objectid, saved as a value of _id. The value of _id can be any type, except the array, in the actual application, the user is encouraged to define their own _id value, but to ensure its uniqueness. There are two scenarios as follows:
2.1 Sequence Numbers: Serial number
Traditional database, usually with an increment sequence to provide the primary key, in MongoDB with Objectid instead, we can get the primary key by the following function: Functions counter (name) {var ret = Db.counters.findAndModify ({query:{_id:name}, update:{$inc: {next:1}}, "New": True, upsert:true}); return ret.next; } db.users.insert ({_id:counter ("users"), Name: "Sarah C."})//_id:1 Db.users.insert ({_id:counter ("users"), Name: "Bob D ."}) _id:2
2.2 Using UUID
If the UUID is used to provide the primary key, our application needs to generate the UUID itself, considering the efficiency, it is recommended to save the UUID to the Bson bindata type, if the use case is not very high in efficiency, it can be saved as a string type.
3. Database Association
In MongoDB, there are usually two kinds of association habits, one is simple manual association, the other is using Dbref.
3.1 Simple manual correlation//Find
> Db.post.save ({title: ' MongoDB Manual ', Author: ' Sam '});
> P = db.post.findOne (); {"_id": ObjectId ("4de36b33282677bdc555a83a"), "title": "MongoDB Manual", "Author": "Sam"}//Association
> Db.authors.findOne ({name:p.author}); {"_id": ObjectId ("4de36c14282677bdc555a83b"), "name": "Sam", "age": +, "email": "sanlai_lee@lisanlai.cn"} 3.2 using Dbre F Association
Dbref Association Syntax:
{$ref: <collname>, $id: <idvalue>[, $db: <dbname>]}
Example:
> x = {name: ' biology '} {"name": "Biology"}
> Db.courses.save (x)
> x {"Name": "Biology", "_id":
ObjectId ("4B0552B0F0DA7D1EB6F126A1")}
> stu = {name: ' Joe ', classes: [New Dbref (' courses ', x._id)}//or we could write://stu = {name: ' Joe ', Clas SES: [{$ref: ' Courses ', $id: x._id}]}
> Db.students.save (STU)
> Stu {"name": "Joe", "classes": [{"$ref": "Courses", "$id": ObjectId ("4B0552B0F0DA7D1EB6F126A1")}], "_id": O Bjectid ("4B0552E4F0DA7D1EB6F126A2")}
> Stu.classes[0] {"$ref": "Courses", "$id": ObjectId ("4B0552B0F0DA7D1EB6F126A1")}
> Stu.classes[0].fetch () {"_id": ObjectId ("4B0552B0F0DA7D1EB6F126A1"), "name": "Biology"