Last active
December 12, 2015 01:09
-
-
Save ronalstal/4689387 to your computer and use it in GitHub Desktop.
mongo shell script for [mongodb-users] Backwards $all query...?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// reference: https://groups.google.com/forum/?fromgroups=&hl=pt-BR#!topic/mongodb-user/h2N5v8gwi6M | |
// | |
db.recip.drop(); | |
db.recip.insert({_id:"B",i:[1,2,3,4,5,6]}); | |
db.recip.insert({_id:"C",i:[1,2,3,6,9,8]}); | |
db.recip.insert({_id:"D",i:[1,2,3]}); | |
A = [1,2,3,4]; | |
qry = {}; | |
qry.$and = []; | |
for ( i in A ) { | |
p = {}; p.i = A[i]; | |
qry.$and.push(p); | |
} | |
// printjson(qry); | |
print("\nfinds B"); | |
db.recip.find(qry).forEach(function(doc){printjson(doc)}); | |
print("\n"); | |
// | |
// with substitutes | |
// | |
db.subst.drop(); | |
db.subst.insert({_id:4,s:[9,11]}) | |
qry={}; | |
qry.$and = []; | |
for (i in A) { | |
subs = db.subst.findOne({_id:A[i]}); | |
if ( subs == null ) { | |
p = {}; p.i = AS[i]; | |
qry.$and.push(p); | |
} else { | |
ASx = {}; ASx.$or = []; | |
p = {}; p.i = A[i]; | |
ASx.$or.push(p); | |
for ( i in subs.s ) { | |
p = {}; p.i = subs.s[i]; | |
ASx.$or.push(p); | |
} | |
qry.$and.push(ASx); | |
} | |
} | |
//printjson(qry); | |
print("\nfinds B and C"); | |
db.recip.find(qry).forEach(function(doc){printjson(doc)}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment