I’ve read and searched about MongoDB’s JSON-BSON constructions but I do not understand (could not find either) how to have nested data and how to query it.
What I’d like to learn is, if somebody wants to store array within an array as in:
id: x,
name: y,
other: z,
multipleArray: [
(lab1: A, lab2: B, lab3:C),
(lab1: AB, lab2: BB, lab3:CB)
(lab1: AC, lab2: BC, lab3:CC)
..
..
]
How to store such data and then retrieve some, a specific or all elements of “multipleArray” contents?
Any resource on the subject would also be highly appreciated.
Bryan had some great advice which you should heed.
Also, as Manoj said, what you actually have is an array of objects. The following might help you out a bit…
Lists are just ordered sequences:
[1,2,3...]or[2,292,111]The first element in the last example is 2, the second is 292… lists/arrays are denoted by square brackets
[ ]Objects map keys to values:
{ name: "Tyler", age: 26, fav_color: "green" }name maps to “Tyler”, age maps to 25, etc… and objects are denoted by braces
{ }A document in mongodb is an object. So, like above, they map keys to values. Those values can be strings, numbers, arrays… or other even other (nested) objects)
So, lets take a look at your document. You have an object (document) that has keys id, name, other, and multipleArray. The value multiple array maps to is an array
[ ]of Objects{ }.MongoDB has this feature called multikeys, it basically takes the value you are querying for and tries to match it against every value in the array.
If you wanted to find the document where
multipleArraycontained the document{lab1: "A", lab2: "B", lab3: "C"}, you query like this:db.data.find({multipleArray: {lab1: "A", lab2: "B", lab3: "C"}})I’m assuming x, y, and z are defined already.
There are more subtleties and complexities, but if you want to learn more read the documentation on the mongodb site here or get a book.