One of the most powerful features of Document based NoSQL databases is the ability to nest arrays of objects inside the parent objects. This is essentially the main way that you can simulate the equivalent of relationships and referential integrity.
Looking at the sample data we have been using on this website so far:
[ {"_id":1, "FoodItem":"Chocolate Frogs", "Description":"literally chocolate shaped like a frog that jumps around - comes with a wizard card", "Price":1.2, "Inventory":56}, {"_id":2, "FoodItem":"Bertie Bots Every Flavour Beans", "Description":"Basic jelly beans made to taste like just about anything", "Price":12.5, "Flavours":["Pear","Peppermint","Rocky Road","Salt","Soap","Toothpaste","Vanilla","Watermelon"]}, {"_id":3, "FoodItem":"Pumpkins Pasties", "Description":"Pastries made to taste like pumpkins", "Price":1.8, "Inventory":14}, {"_id":4, "FoodItem":"Cauldron Cakes", "Description":"Cauldron shaped licorice flavoured sweets", "Price":1.3}, {"_id":5, "FoodItem":"Licorice Wands", "Price":0.9, "Colour":"Black"} ]
it can be seen that the flavours included in FoodItem with _id 2 is an array embedded, indicated by the [] square bracket notation, inside that food item. These flavours will only apply to the parent food item and will not be associated with other food items. Using a series of array modifiers, you will be able to add to, remove from and find various elements within the array.
There are modifiers, similar to JavaScript, for manipulating arrays of documents.
The "$push" operator adds elements to the end of an array if the array exists. If the array does not exists it will be created with the given elements.
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "..."
}
The following command adds an array comment to the existing document.
> db.blog.posts.update({"title" : "A blog post"}, ... {"$push" : {"comments" : ... {"name" : "joe", "email" : "joe@example.com", ... "content" : "nice post."}}})
Since the “comments” key does not exist, it will be created
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "...",
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
}
]
}
Let's add another comments, but this time the array exists, so it will be added to the end.
> db.blog.posts.update({"title" : "A blog post"},
... {"$push" : {"comments" :
... {"name" : "bob", "email" : "bob@example.com",
... "content" : "good post."}}})
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "...",
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
]
}
The “$each” operators is used to push multiple values to an array on one “$push” operation.
> db.stock.ticker.update({"_id" : "GOOG"}, ... {"$push" : {"hourly" : {"$each" : [562.776, 562.790, 559.123]}}})
Three elements are pushed into the array.
The $slice operator is used with the “$push” operator to make sure that an array will not grow bigger than a certain size.
> db.movies.find({"genre" : "horror"}, ... {"$push" : {"top10" : { ... "$each" : ["Nightmare on Elm Street", "Saw"], ... "$slice" : -10}}})
The value of the $slice limits the array to hold 10 elements. If the number of pushing elements violates the size, only the last 10 elements added to the array will be kept.
You can sort an array before pushing more elements to the array.
> db.movies.find({"genre" : "horror"}, ... {"$push" : {"top10" : { ... "$each" : [{"name" : "Nightmare on Elm Street", "rating" : 6.6}, ... {"name" : "Saw", "rating" : 4.3}], ... "$slice" : -10, ... "$sort" : {"rating" : -1}}}})
Before pushing the new elements, all elements existing in the array and the new ones is sorted by the “rating” key and the first 10 elements from the sorted list will be stored into the array.
coming soon......
coming soon......