0

I try to make a query and i don't know the right way to do this. The mongo collection structure contains multiples user ID (uid) and i want to make a query that get all datas ("Albums") where the User ID match one of the uid.

I do not know if the structure of the collection is good for that and I would like to know if I should do otherwise.

{
  "_id": ObjectId("55814a9799677ba44e7826d1"),
  "album": "album1",
  "pictures": [
    "1434536659272.jpg",
    "1434552570177.jpg",
    "1434552756857.jpg",
    "1434552795100.jpg"
  ],
  "uid": [
    "12814a8546677ba44e745d85",
    "e745d677ba4412814e745d7b",
    "28114a85466e745d677d85qs"
  ],
  "__v": 0
}

I just searched on internet and found this documentation http://docs.mongodb.org/manual/reference/operator/query/in/ but I'm not certain that this is the right way.

In short, I need to know: if I use the right method for the stucture of the collection and the operator "$in" is the right solution (knowing that it may have a lot of "User ID": between 2 and 2000 maximum).

2 Answers 2

1

You don't need $in unless you are matching for more than one possible value in a field, and that field does not have to be an array. $in is in fact shorthand for $or.

You just need a simple query here:

Model.find({ "uid": "12814a8546677ba44e745d85" },function(err,results) {

})

If you want "multiple" user id's then you can use $in:

Model.find(
    { "uid": { "$in": [
        "12814a8546677ba44e745d85",
        "e745d677ba4412814e745d7b",
    ] } },
    function(err,results) {

    }
)

Which is short for $or in this way:

Model.find(
    { 
        "$or": [
            { "uid": "12814a8546677ba44e745d85" },
            { "uid": "e745d677ba4412814e745d7b" }
        ]
    },
    function(err,results) {

    }
)
Sign up to request clarification or add additional context in comments.

4 Comments

"$in" is a shorthand for "$or"? Why would you think that?
@SergioTulentsev Is the example not clear? $in is an $or comparison without needing to specify the field again in different query documents. Match this "or" this. Not hard to understand
They sure behave similarly, but, IMO, "is a shorthand for" is too much of a statement. Implies certain implementation details.
@SergioTulentsev It's a clear example of what it means. Clearly you cannot use $in with multiple fields, so then you use $or. For a single field you use $in rather than type out the "long form" as is shown. Sound like shorthand to me. Anyhow not a debating area. If things are unclear to you then please ask your own question and someone will possibly take the time to explain it to you.
0

Just to answer your question, you can use the below query to get the desired result.

db.mycollection.find( {uid : {$in : ["28114a85466e745d677d85qs"] } } )

However, you need to revisit your data structure, looks like its a Many-to-Many problem and you might need to think about introducing a mid collection for that.

2 Comments

Of course i need to "link" the user collection with "album" collection. Is there a relation in mongoose like BelongsTo or BelongsToMany ... ? What is the right procedure to do that ?
As per my knowledge there is nothing built in for these relationships, and moreover there is generically nothing right (or wrong). It all depends on your requirements. you can look this blog how he is maintaining the relationship, blog.markstarkman.com/blog/2011/09/15/… however, I would have actually created a separate collections to maintain the relationships and the actual entities in their respective collections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.