In MySQL, or almost any other RDBMS, it is common to have an ID for the row (object), so you use it in other tables to make a link between the tables (Relations, or PK and FK if you prefer). This is part of the normalization process that is recommended for most types of DB design [there are exceptions though].
In MongoDB however, the general way or design is to denormalize. i.e. the document (row) is stored with all its related objects.
To make it more clear:
In MySQL you would have two tables: cars
and car_types
. In cars you only store the ID of the type of this car.
In Mongo DB, you would have one collection called car
s, and the document in this collection would have a sub-document that contains the type's info. (You still can have a separate collection for types
, but you don't store only objectID
in car's collection)
HTH
I accomplished it with your criteria which was to use the tags field in your query. I of course had to filter on the document further to only update that specific document.
I presumed other documents may have a tag of "famous". I created two documents with tags:famous to prove this worked when updating 1 document. Here's my solution with comments.
//basics
db = test
collection = test
//data looked like this with two documents for testing
/* 0 */
{
"_id" : ObjectId("5498778567bbbc2fff723515"),
"articleId" : [
{
"articleId" : "9514666",
"articleCount" : 1
}
],
"count" : 1,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}
/* 1 */
{
"_id" : ObjectId("5498781567bbbc2fff723516"),
"articleId" : [
{
"articleId" : "951466622",
"articleCount" : 22
}
],
"count" : 1,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}
//confirmed I can query the one document like this
db.test.find({'tags': 'famous','articleId.articleId':'9514666'})
//was able to execute an update as described
db.test.update({'tags': 'famous','articleId.articleId':'9514666'}, {"articleId" : [
{
"articleId" : "9514666",
"articleCount" : 4
},
{
"articleId" : "9514667",
"articleCount" : 3
}
],
"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
})
//Resulting update to document is
/* 0 */
{
"_id" : ObjectId("5498778567bbbc2fff723515"),
"articleId" : [
{
"articleId" : "9514666",
"articleCount" : 4
},
{
"articleId" : "9514667",
"articleCount" : 3
}
],
"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}
//showing both documents, only first one updated obviously
/* 0 */
{
"_id" : ObjectId("5498778567bbbc2fff723515"),
"articleId" : [
{
"articleId" : "9514666",
"articleCount" : 4
},
{
"articleId" : "9514667",
"articleCount" : 3
}
],
"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}
/* 1 */
{
"_id" : ObjectId("5498781567bbbc2fff723516"),
"articleId" : [
{
"articleId" : "951466622",
"articleCount" : 22
}
],
"count" : 1,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}
Best Answer
'some test', so let's say you want to add 10 to 96 :
In the find part, you point to the courses with name 'Algebra'. The
.$
points you to that found element. Of that found element, it will change the second (.1
) value in the grades array. Using$
, you don't need to know the position of the course in the array.