简体   繁体   中英

MongoDB Casbah Query DSL $exists with $in

Trying to convert the following mongo console query into casbah/scala's DSL.

db.campaign.find({ "space.id": { $exists: true, $in: ["123","456"] } })

The best I have come up with has been the following which uses an or instead.

$or("space.id" $exists false, "space.id" $in restrictedSpaces)

Just looking for someone with more experience (read any), to say that this is the correct way to do this, or that there is a better way to go about this.

Thanks, Aaron

Aha..

My apologies for posting what may have been a silly question. The answer seems to be that doing what I did is not necessary..

Mongo seems to be smart enough to ignore the $in when the field doesn't exist while I expected it to throw an error informing me that the document did not have the field and hence my original question.

The following, simple, query works and illustrates the simplicity and performance of the DSL and of mongo.

"space.id" $in restrictedSpaces

Note: restrictedSpaces is a List of String.

Thanks, Aaron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM