sorting - Mongodb sort on conditional clause -
i want this:-
db.pub_pairs.find({status:'active'}) .sort( { if (pcat="abc") 0 else 1 end }) .limit(10) in other words, want prefer records field pcat "abc". need fast, can add indexes necessary. suggestions on how this? thanks
a decently efficient way of doing query db twice. once documents having pcat="abc" , once (if needed) documents having pcat!="abc"
something along lines of (pseudo-code):
var data = db.pub_pairs.find({status:'active', pcat="abc"}) .limit(10) if (data.length < 10) data.concat(db.pub_pairs.find({status:'active', pcat!="abc"}) .limit(10-data.length)) for perform well, suggest compound index on {status: 1, pcat: 1}
please note, like other "find" mongodb, have prepared retrieve same document twice in result set, notably here if pcat field concurrently modified other client during execution.
Comments
Post a Comment