Q: How do you traverse a MongoDB Collection of complex documents?
A: db.stories.find({},{tags:true}).forEach(function(v){for(var i=0;i < v["tags"].length;i++){print(v["tags"][i])}})
Here is the collection, stories, against which you can run the JavaScript shown above.
[{"_id":{"$oid":"4e9cea4d44ae29e89c65fc28"},
"tags":["pcvim",
"twzbl",
"gajpv"],
"title":"teoqc ccqnupnqnf",
"email":"bob@cogarttech.com",
"description":"pffafckratlladvyvmasqrkknnjfkj",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc29"},
"tags":["awdjq",
"gprzp",
"usnmd"],
"title":"iclih iqkfobaetk",
"email":"bob@cogarttech.com",
"description":"ukuvdaqtejnpmzhgnkhmqqewcaeqch",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2a"},
"tags":["wofcd",
"mslmt",
"zmyec"],
"title":"uhbbu zvztgyqfsv",
"email":"bob@cogarttech.com",
"description":"vshlhdsbtodolvboympvzbidyuvdch",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2b"},
"tags":["mzged",
"ewhwu",
"lcbwe"],
"title":"hnpjy akizruqepj",
"email":"bob@cogarttech.com",
"description":"ybdpfhlfonkgoqbuousqszcfgbgjab",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2c"},
"tags":["blugp",
"zbsye",
"rmfvp"],
"title":"dbzwa slnnqwpgkk",
"email":"bob@cogarttech.com",
"description":"uwlqaardkcewhgdrclqqppruocemgb",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2d"},
"tags":["lfcrv",
"nojgb",
"shdaw"],
"title":"ybyqk qpmubqjhcb",
"email":"bob@cogarttech.com",
"description":"tsceoepdfczbkmrmyabnlelngacfyv",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2e"},
"tags":["uqoal",
"grwhy",
"fvijk"],
"title":"qyfbk webdlhgnvt",
"email":"bob@cogarttech.com",
"description":"tswcepjcgmbwybwiiauaogsupgyekq",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2f"},
"tags":["brtmf",
"jnwso",
"alhef"],
"title":"nwyjb fywupddvyq",
"email":"bob@cogarttech.com",
"description":"jdisibfkwqsnfysmgcrunvflcrkfca",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc30"},
"tags":["jpvqr",
"kdwdy",
"tlnsg"],
"title":"fvths itzfjwlibg",
"email":"bob@cogarttech.com",
"description":"kfvnwulgchlbzzlntlccoebpstssjc",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc31"},
"tags":["supiy",
"ceqdb",
"eryyr"],
"title":"gzist ytikusfopj",
"email":"bob@cogarttech.com",
"description":"cowehuanouftyvwlrsqhrnpjbhheuq",
"lastname":"Reselman",
"firstname":"Bob"}]
Here is the output:
pcvim
twzbl
gajpv
awdjq
gprzp
usnmd
wofcd
mslmt
zmyec
mzged
ewhwu
lcbwe
blugp
zbsye
rmfvp
lfcrv
nojgb
shdaw
uqoal
grwhy
fvijk
brtmf
jnwso
alhef
jpvqr
kdwdy
tlnsg
supiy
ceqdb
eryyr
As you might imagine, I've taken an interest in MongoDB. I don't know if I like it yet, given that it is in a way sorta of a throw back to IMS. But, hey, maybe progress is more about rewriting an old story than not.
Here is the collection, stories, against which you can run the JavaScript shown above.
[{"_id":{"$oid":"4e9cea4d44ae29e89c65fc28"},
"tags":["pcvim",
"twzbl",
"gajpv"],
"title":"teoqc ccqnupnqnf",
"email":"bob@cogarttech.com",
"description":"pffafckratlladvyvmasqrkknnjfkj",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc29"},
"tags":["awdjq",
"gprzp",
"usnmd"],
"title":"iclih iqkfobaetk",
"email":"bob@cogarttech.com",
"description":"ukuvdaqtejnpmzhgnkhmqqewcaeqch",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2a"},
"tags":["wofcd",
"mslmt",
"zmyec"],
"title":"uhbbu zvztgyqfsv",
"email":"bob@cogarttech.com",
"description":"vshlhdsbtodolvboympvzbidyuvdch",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2b"},
"tags":["mzged",
"ewhwu",
"lcbwe"],
"title":"hnpjy akizruqepj",
"email":"bob@cogarttech.com",
"description":"ybdpfhlfonkgoqbuousqszcfgbgjab",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2c"},
"tags":["blugp",
"zbsye",
"rmfvp"],
"title":"dbzwa slnnqwpgkk",
"email":"bob@cogarttech.com",
"description":"uwlqaardkcewhgdrclqqppruocemgb",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2d"},
"tags":["lfcrv",
"nojgb",
"shdaw"],
"title":"ybyqk qpmubqjhcb",
"email":"bob@cogarttech.com",
"description":"tsceoepdfczbkmrmyabnlelngacfyv",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2e"},
"tags":["uqoal",
"grwhy",
"fvijk"],
"title":"qyfbk webdlhgnvt",
"email":"bob@cogarttech.com",
"description":"tswcepjcgmbwybwiiauaogsupgyekq",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc2f"},
"tags":["brtmf",
"jnwso",
"alhef"],
"title":"nwyjb fywupddvyq",
"email":"bob@cogarttech.com",
"description":"jdisibfkwqsnfysmgcrunvflcrkfca",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc30"},
"tags":["jpvqr",
"kdwdy",
"tlnsg"],
"title":"fvths itzfjwlibg",
"email":"bob@cogarttech.com",
"description":"kfvnwulgchlbzzlntlccoebpstssjc",
"lastname":"Reselman",
"firstname":"Bob"},
{"_id":{"$oid":"4e9cea4d44ae29e89c65fc31"},
"tags":["supiy",
"ceqdb",
"eryyr"],
"title":"gzist ytikusfopj",
"email":"bob@cogarttech.com",
"description":"cowehuanouftyvwlrsqhrnpjbhheuq",
"lastname":"Reselman",
"firstname":"Bob"}]
Here is the output:
pcvim
twzbl
gajpv
awdjq
gprzp
usnmd
wofcd
mslmt
zmyec
mzged
ewhwu
lcbwe
blugp
zbsye
rmfvp
lfcrv
nojgb
shdaw
uqoal
grwhy
fvijk
brtmf
jnwso
alhef
jpvqr
kdwdy
tlnsg
supiy
ceqdb
eryyr
As you might imagine, I've taken an interest in MongoDB. I don't know if I like it yet, given that it is in a way sorta of a throw back to IMS. But, hey, maybe progress is more about rewriting an old story than not.
<< Home