简体   繁体   中英

mongoDb query to search values using $in and $or?

var norm = [ 'may', 'funny', 'top funny', 'dinner', 'dog', 'hello', 'flo', 'sake', 'hai', 'video', 'rhymes', 'fine', 'good', 'public', 'testing', 'irish', 'hii', 'babies', 'coffee', 'happy', 'working', 'comedy', 'india', 'best', 'top', 'feelings', 'krishna', 'test' ]

schema.find( { $or: [ {"head":{"$in":norm}}, {"key":{"$in":norm}} ],"privacy":{"$ne":"Private"} },function(err,res){});

i have to list the result in ascending order as per the norm but i didnt get the exact result from the above query.

That query wont ever work, this is the format for find schema. db.collection.find(query, projection) where projection are the fields you wish to return from the query if it is a success. Also in your query is both norm is a array right?

Your is wrong: schema.find( { $or: [ {"head":{"$in":norm}}, {"key":{"$in":norm}} ],"privacy":{"$ne":"Private"} },function(err,res){}); .

This "privacy":{"$ne":"Private"} is not included in the query that is why, it is in the projection section.

should be something like this : schema.find( { $or: [ {"head":{"$in":norm}}, {"key":{"$in":norm}}],"privacy":{"$ne":"Private"}}); .

Here is a elegant way for this query.

Schema.find( { $or: [ {"head":{"$in":norm}}, {"key":{"$in":norm}}]).where("privacy").ne("Private").exec(function(err,doc){});

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