Can DBRefs only look up one level in mongodb?

  mongodb, question

Build three sets, namely student, score and join


The results are as follows:

 "_id" : ObjectId("59813e997471c10b2e00838e"),
 "name" : "xiaomei",
 "phone" : 13212345680,
 "gender" : "girl"
 db.score.insert({Sid:[new DBRef('student',ObjectId("59813e737471c10b2e00838e"))],math:97,chinese:83})

The results are as follows:

 "_id" : ObjectId("598140327471c10b2e008393"),
 "Sid" : [
 DBRef("student", ObjectId("59813e737471c10b2e00838e"))
 "math" : 97,
 "chinese" : 83
 db.join.insert({Sid:[new DBRef('score',ObjectId("598140327471c10b2e008393"))],isJoin:false})

The results are as follows:

 "_id" : ObjectId("598140ec7471c10b2e008394"),
 "Sid" : [
 DBRef("score", ObjectId("598140327471c10b2e008393"))
 "isJoin" : false

db.student.findOne({"_id":db.score.findOne().Sid[0].$id})There are results
db.student.findOne({"_id":db.score.findOne({"Sid":db.join.findOne().Sid[0].$id})})The result was null, I don’t know where there is a problem, please correct me? Is this reference using DBRefs useful in actual project development? Is there any good document to recommend?

Looking for a way to JOIN in MongoDB may be in the wrong direction. It’s not that MongoDB doesn’t want to support JOIN. The problem is that JOIN cost is too high in a distributed environment. If performance and JOIN are the two choices, JOIN is the one to be abandoned. After all, JOIN can be replaced and performance problems are not so easy to solve.
The above is a digression. As far as your problem is concerned, is there any special reason not to putscorePut it onstudentInside? In this way, all the results can be read out at once.

 name: "xiaomei",
 phone: 13212345680,
 gender: "girl",
 scores: [
 {math: 97},
 {chinese: 83}

You can read the official documents about the design of MongoDB data model.Data Model Design.
For DBRef, performance is not cost-effective. If you insist on using it, check the query carefully:


There is no result becauseDBRef("student", ObjectId("59813e737471c10b2e00838e"))ReferencedObjectIdAndstudenthit the targetObjectId("59813e997471c10b2e00838e")Not the same (look carefully at the middle). Therefore, the second article is unlikely to have any results.