0

How do a query to find by Id using DBRef in MongoDB ?

The documents of Student collection are:

{
    "_id" : ObjectId("59bc49ef962d7420843331b7"),
    "first_name" : "Anthony",
    "last_name" : "McKelly",
}
{
    "_id" : ObjectId("59bc49f0962d74198d421a6d"),
    "first_name" : "John",
    "last_name" : "Doe",
}
{
    "_id" : ObjectId("59bc49f0962d742048757807"),
    "first_name" : "Peter",
    "last_name" : "Parquer",
}
{
    "_id" : ObjectId("59bc49f0962d742048757818"),
    "first_name" : "Katy",
    "last_name" : "Jackson",
}
{
    "_id" : ObjectId("59bc49f0962d74204d270ff3"),
    "first_name" : "Mary",
    "last_name" : "Petrova",
}

This is a part of the Queue collection:

{
    "_id" : ObjectId("59bc49ef962d7420843331c6"),
    "student" : DBRef("students", ObjectId("59bc49ef962d7420843331b7"), "school"),
    "groupid" : "59aeda24962d740934127b82"
}
{
    "_id" : ObjectId("59bc49f0962d74198d421a7c"),
    "student" : DBRef("students", ObjectId("59bc49f0962d74198d421a6d"), "school"),
    "groupid" : "59aeda24962d740934127b82"
}
{
    "_id" : ObjectId("59bc49f0962d742048757816"),
    "student" : DBRef("students", ObjectId("59bc49f0962d742048757807"), "school"),
    "groupid" : "59aeda24962d740934127b82"
}
{
    "_id" : ObjectId("59bc49f0962d742048757827"),
    "student" : DBRef("students", ObjectId("59bc49f0962d742048757818"), "school"),
    "groupid" : "59aeda24962d740934127b82"
}
{
    "_id" : ObjectId("59bc49f0962d74204d271002"),
    "student" : DBRef("students", ObjectId("59bc49f0962d74204d270ff3"), "school"),
    "groupid" : "59aeda24962d740934127b82"
}

I need know how access to student data across the DBRef in the Queue collection using a query.

This not work:

db.queue.find({student.id : '59bc49ef962d7420843331b7' })
db.queue.find({student._id : ObjectId("59bc49f0962d742048757807") }) 

Please any help I am very grateful.

  • This should help - https://stackoverflow.com/questions/31753058/mongodb-query-on-dbref-type – vijaykrishnavanshi Oct 23 '17 at 23:49
  • The lesson here should be "Don't use `DBRef`". Whilst not "officially" deprecated, it's general usage is depleted and it always was a bad idea. The best advice ( and especially considering that all references here link to the same collection ) is to convert these to use just the `ObjectId` instead. The detail of the "related collection" belongs in your "client side schema" instead, and simply defines "where to look" for the values contained. `DBRef` is just not supported by modern features to "join" such as [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) – Neil Lunn Oct 24 '17 at 00:11
  • So like **ALL** BSON Types. The only way to "match" a `DBRef` is to supply it as the BSON Type. `db,queue.find({ student: DBRef("students", ObjectId("59bc49ef962d7420843331b7"), "school") })`. Or in environments that support `db.queue.find({ "student.$id": ObjectId("59bc49ef962d7420843331b7") })`. Just like `"1"` does not match `1` because it's not the same BSON Type. Refactor to not use `DBRef` – Neil Lunn Oct 24 '17 at 00:13

0 Answers0