0

Basically, the end goal is to provide a default scope to all my DB queries. This means limiting all queries to a set of object IDs. My question is, how can I query by ID and make sure that the query is limited to my list of Object IDs?

I'd like to do something like this:

db.posts.findOne({_id: ObjectId("50eb4a874947fb00e3cc0612")})
        .where({_id: { $in: [ObjectId("50eb4a874947fb00e3cc0610"),ObjectId("50eb4a874947fb00e3cc0611")] }});

As You can see, the query should return no results because the queried ID is not in the $in array of object IDs.

Btw, the above query just gives me an error saying db.posts.findOne({_id: ObjectId("50eb4a874947fb00e3cc0612")}).where is not a function (shell):1

4

1 回答 1

1

这听起来很奇怪,但您可以尝试$andhttp://docs.mongodb.org/manual/reference/operators/#_S_and)将这两个子句组合起来,例如:

db.col.find({
    $and:[
        {_id: ObjectId("50eb4a874947fb00e3cc0612")},
        {_id: {
            $in: [
                ObjectId("50eb4a874947fb00e3cc0610"),
                ObjectId("50eb4a874947fb00e3cc0611")
            ]
         }}
     ]
 });

这是我相信的一种方法。

于 2013-01-07T22:56:42.703 回答