Xiao continued to learn about the basic use of MongoDB foreign keys.
Methods to represent table relationships
In traditional relational databases, table relationships are represented and data is refined by indexes. In MongoDB, table relations are represented by nesting, that is, a document is nested with a document, as the association between two documents of MongoDB, and reference link is used as the association between documents.
Document the nested
Use visual
Enter the following document objects using a visual editor as a nested document
{
"ming": "ming"."ming2": {
"ming3": "ming8"}}Copy the code
After successful insertion, the following is displayed
This completes nesting of documents, that is, representing an association between two documents.
Using the JDK
The JDK is used here to connect. First add dependencies
< the dependency > < groupId > org. Mongo < / groupId > < artifactId > mongo - driver < / artifactId > < version > 3.5.0 < / version > </dependency>Copy the code
To connect
MongoClient = new MongoClient(MongoClient = new MongoClient)"106.53.115.12", 27017); MongoDatabase = mongoclient.getDatabase ("koa");
System.out.println("Connect to database successfully");
}catch(Exception e){
System.err.println( e.getClass().getName() + ":" + e.getMessage() );
}
Copy the code
To insert
List<Document> collections = new ArrayList<Document>();
Document d1 = new Document();
d1.append("name".Romance of The Three Kingdoms).append("author"."Luo Guanzhong");
Document d2 = new Document();
d2.append("name".A Dream of Red Mansions).append("author", d1);
collections.add(d2);
c.insertMany(collections);
Copy the code
The query data is as follows
{
"name" : A Dream of Red Mansions."author": {
"name": Romance of The Three Kingdoms."author": "Luo Guanzhong"}}Copy the code
At this point, the nesting of documents is complete
Foreign key query
Use JS language, query association
Use new DBRef for foreign key query. The following fields are now available for DBRef.
$ref
该$refThe field contains the name of the collection in which the reference document is located.$id
该$idThe field contains the _id reference to the value of the field in the document.$dbOptional. Contains the name of the database in which the reference document resides. Only some drivers support it$dbReference, the field description can be associated across collectionsCopy the code
Here the collection operations are associated as follows
Var a={value:"1"}
> var b={value:"2"}
> var c={value:"9"}
> var d={value:"10"} > db. A.s ave > db (a). The A.s ave (b) > db. A.s ave (c) > db. A.s ave (d) / / collection data query > db. A.find()
{ "_id" : ObjectId("4e3f33ab6266b5845052c02b"), "value" : "1" }
{ "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" }
{ "_id" : ObjectId("4e3f33e06266b5845052c02d"), "value" : "9" }
{ "_id" : ObjectId("4e3f33e26266b5845052c02e"), "value" : "10" }
Copy the code
To carry out the association of sets, the new DBRef method is used to complete the association of sets
In this case, new DBRef is used as the keyword. A is key and ObjectId is value. > var Ba={Apid:[new DBRef('A',ObjectId("4e3f33de6266b5845052c02c")), value: 3} / collection/save > db. B.s ave (Ba) > var Ba = {Apid: [new DBRef ('A',ObjectId("4e3f33de6266b5845052c02c"))],value:4}
> db.B.insert(Ba)
> var Ba={Apid:[new DBRef('A',ObjectId("4e3f33de6266b5845052c02c"))],value:7}
> db.B.insert(Ba)
> var Ba={Apid:[new DBRef('A',ObjectId("4e3f33de6266b5845052c02c")), value: 8} > db. B.i nsert (Ba) / / query > db. B. ind () / / as you can see, has successfully associated {"_id" : ObjectId("4e3f3dd96266b5845052c035"), "Apid": [{"$ref" : "A"."$id" : ObjectId("4e3f33de6266b5845052c02c")},"value": 3} {"_id" : ObjectId("4e3f3de16266b5845052c036"), "Apid": [{"$ref" : "A"."$id" : ObjectId("4e3f33de6266b5845052c02c")},"value": 4} {"_id" : ObjectId("4e3f3dec6266b5845052c037"), "Apid": [{"$ref" : "A"."$id" : ObjectId("4e3f33de6266b5845052c02c")},"value": 7} {"_id" : ObjectId("4e3f3df06266b5845052c038"), "Apid": [{"$ref" : "A"."$id" : ObjectId("4e3f33de6266b5845052c02c")},"value"8} :Copy the code
Use mongo-Java
DBRef refB = new DBRef(db,"transations", obj.get("_id")); // Create a new set DBObject subObj = new BasicDBObject(); // Insert subobj. put("brand", refB); // Save accounts. Save (subObj);Copy the code
The query result is as follows