I have an object assigned to a variable, such as:
var child = {
{
_id: 'ObjectID("16e7f7baa05c3a9d751363")',
title: 'Testing',
sort: 3,
active: true
}
The following does not work, and honestly I am unsure of what I'm missing:
db.category.update(
{ _id: "thisistheparentdocumentsid", "children._id": child._id },
{
$set: {
"children.$.title": child.title,
"children.$.active": child.active,
"children.$.sort": child.sort
}
}
);
What I really want to do, as I want to reuse this method and not repeat myself later is:
db.category.update(
{ _id: "thisistheparentdocumentsid", "children._id": child._id },
{
$set: {
"children" : child
}
}
);
That doesn't work either. I referenced this post: Update array element by id with mongo query
As well as: http://docs.mongodb.org/manual/reference/operator/update/positional/
But my query is not working. Am I missing something elementary here?
--- Edit ---
Here is the result when I query based on the parent document
db.category.findOne({ "_id" : "9dYgKdfFczgiRcNouij"});
{
"title" : "ParentTest",
"active" : true,
"children" : [
{
"_id" : ObjectId("680d55c6995ef6f0748278c2"),
"title" : "ChildTest",
"active" : true
},
{
"_id" : ObjectId("2b4469c1a4c8e086942a1233"),
"title" : "ChildTest2"
"active" : true
}
],
"_id" : "9dYgKdfFczgiRcNouij"
}
I'm passing data to the server, such as... Method.call("UpdateCommand", id, child);
The variable id is the parent id of the document, and the child is the object I mentioned.
I use child._id.toString() to generate the matching ObjectId("...");
So more specifically:
db.category.update(
{ _id: id, "children._id": child._id.toString() },
{
$set: {
"children.$.title": child.title,
"children.$.active": child.active,
"children.$.sort": child.sort,
"children.$.uppercase": child.title.toUpperCase()
}
}
);
Yet this doesn't work. I'm thinking something is off with my selector, but I can use findOne() with the same method and it does return the proper document, and using $elemMatch returns the specified child item in the array.
Anything I might be missing? I've ran all the values through console.log to make sure I'm getting them, and I am.