DBRef Conditional Search in MongoDB 2.6

Asked 2 years ago, Updated 2 years ago, 112 views

For example
In the score collection

{
  "_id"—ObjectId("50f5379785ac77daf3dd4bfd"),
  "user": {
    "$ref": "collection_user",
    "$id"—ObjectId("50f5370e85ac77daf3dd4bfb")
  },
  "score"—100
}

in user collection
{
  "_id"—ObjectId("50f5370e85ac77daf3dd4bfb"),
  "name": "User1",
  "No": 1111
}

When there is data like , we would like to use the user collection in the core collection as an external merge to specify search criteria or sort criteria in name.

If it's mongoDB 3.6 or higher, I feel like I can go there with lookup, but if it's 2.6 or higher, is there any way to achieve it?

mongodb

2022-09-29 21:29

1 Answers

Database server side failed before MongoDB v3.2.Application-side coupling is required.

One application pseudocode image:

 users_rs=db.user.find({name:xxxxxx})
user_id_list = [ ]
for doc in users_rs:
  user_id_list.append(doc._id)
scores_rs=db.scores.find({"user.$id":{$in:user_id_list}})
by merging //scores and users data
// Then sort


2022-09-29 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.