First, the relationship
MongoDB relationships represent the logical interconnectedness of multiple documents. Links can be established between documents through embedding and referencing. The relationship in MongoDB can be: 1 pairs of more than 1, many to many.
A user can use multiple addresses, which is a typical one-to-many relationship.
The user documentation can be:
{ "_id": ObjectId ("52ffc33cd85242f436000001"), "name": "Tom Hanks", " Contact ":" 987654321 ", " dob ":" 01-01-1991 "}
The address document can be:
{ "_id": ObjectId ("52ffc4a5d85242602e000000"), "building": "A, Indiana Apt", "Pincode": 123456, "City": "Los Angeles", "state": "California"
1. Embedded relationship
Using embedded methods, you can embed an address document in a user's document
{ "_id": ObjectId ("52ffc33cd85242f436000001"), "contact": "987654321", " Dob ":" 01-01-1991 ", " name ":" Tom benzamin ", " Address ": [ { " Building ":" A, Indiana Apt " ," pincode ": 123456, " City ":" Los Angeles ", /c17> "state": "California" }, { "building": "A, Acropolis Apt", " Pincode ": 456789, " City ":" Chicago ", " state ":" Illinois "
If you save this way, you can get the address of the user:
Db.users.findOne ({"Name": "Tom benzamin"},{"Address": 1})
The disadvantage of this data structure is that if the user and user address is increasing, the amount of data becomes larger, which will affect the read and write performance.
2. Quoting method
This method is similar to the foreign key in the relational database, and the _id of address is stored in the user document
{ "_id": ObjectId ("52ffc33cd85242f436000001"), "contact": "987654321", " Dob ":" 01-01-1991 ", " name ":" Tom benzamin ", " Address_ids ": [ ObjectId (" 52ffc4a5d85242602e000000 "), ObjectId (" 52ffc4a5d85242602e000001 ") ]}
We can read the object ID (ObjectId) of these user addresses to get the detailed address information of the user. This method requires two queries, the first query of the user address of the object ID (ObjectId), the second time through the ID of the query to obtain the user's detailed address information.
var result = Db.users.findOne ({"Name": "Tom benzamin"},{"Address_ids": 1})var addresses = Db.address.find ({"_id": {"$in": result["Address_ids"]}})
Second, database reference
There are two types of MongoDB references: manual reference (Manual References) and DBRefs
If we store different addresses (Address_home, Address_office, address_mailing, etc.) in different collections (address, Office address, email address, etc.). When we call different addresses, we also need to specify a collection, a document that references a document from multiple collections, and we should use DBRefs.
form of Dbref:
{$ref:, $id:, $db: }
Where $ref: Collection name, $id: Referenced ID, $db: Database name (optional).
The user data document in the following example uses the Dbref, field address:
{ "_id": ObjectId ("53402597d852426020000002"), "Address": { "$ref": " Address_home ", " $id ": ObjectId (" 534009e4d852427820000002 "), " $db ":" W3CSCHOOLCC "} , "contact": "987654321", "dob": "01-01-1991", "name": "Tom benzamin" }
Address The Dbref field specifies that the referenced address document is a W3CSCHOOLCC database under the Address_home collection, with an ID of 534009e4d852427820000002.
In the following code, we look up the user address information for the specified ID in the collection by specifying the $REF parameter (Address_home collection):
var user = Db.users.findOne ({"Name": "Tom benzamin"})var dbref = user.addressdb[dbref.$ Ref].findone ({"_id":(dbref. $id)})
The above instance returns the address data from the Address_home collection:
{ "_id": ObjectId ("534009e4d852427820000002"), "building": "A, Indiana Apt", "Pincode": 123456, "City": "Los Angeles", "state": "California"}
III. Overwrite index query
The overwrite query is the following query:
- All query fields are part of the index
- All the query return fields are in the same index
Because all fields that appear in the query are part of the index, MongoDB does not need to retrieve matching query criteria throughout the data document and return query results that use the same index. Because the index exists in RAM, getting data from the index is much faster than reading the data by scanning the document.
Example: User collection:
{ "_id": ObjectId ("53402597d852426020000002"), "contact": "987654321", " Dob ":" 01-01-1991 ", " gender ":" M ", " name ":" Tom benzamin ", " user_name ":" Tombenzamin "}
Create federated indexes, fields gender and user_name
Db.users.ensureIndex ({gender:1,user_name:1})
The index now overwrites the following query:
Db.users.find ({gender: "M"},{user_name:1,_id:0})
For the above query, MongoDB does not go to the database file to find. Instead, it extracts the data from the index, which is a very fast data query. Since the _id field is not included in our index, _ID is returned by default in the query, and we can exclude it from the query result set in MongoDB. The following instance does not exclude _id and the query will not be overwritten:
Db.users.find ({gender: "M"},{user_name:1})
If all indexed fields are an array, you cannot use an overwrite index query, and all indexed fields are a subdocument.
MongoDB relationships, referencing, overwriting index queries