[DOCS-2446] using $set for arrays nested in arrays Created: 20/Dec/13  Updated: 20/Feb/14  Resolved: 10/Feb/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: mongodb-2.6
Fix Version/s: v1.3.1

Type: Tracking Priority: Major - P3
Reporter: Paul Gribelyuk Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows 7


Participants:
Days since reply: 9 years, 51 weeks, 6 days ago

 Description   

I am trying to update a nested document structure which looks similar to this:
{ k1 : v1,
k2 : [
{inner_k1: inner_v1,
inner_k2: [

{inner_inner_k1: inner_inner_v1, inner_inner_k2: inner_inner_v2 }

]
}
]
}

What is the appropriate way to $set another field in the inner-most document, e.g. inner_inner_k3: inner_inner_v3?

Thanks.



 Comments   
Comment by Githook User [ 20/Feb/14 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-2446: removing merge artifacts
Branch: v2.4
https://github.com/mongodb/docs/commit/7381226f0bb403ff35da0facf4b07ae68c238993

Comment by Githook User [ 10/Feb/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2446 positional operator and nested arrays
Branch: v2.4
https://github.com/mongodb/docs/commit/ff48e65fe7be1c1c971d90b4e3ee9f4313f7f963

Comment by Githook User [ 10/Feb/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2446 positional update operator and nested array
Branch: master
https://github.com/mongodb/docs/commit/357d1585760864c36369afbbbcd2df383611a059

Comment by Kay Kim (Inactive) [ 16/Jan/14 ]

Hi Paul – there's a change to the positional document that's undergoing review that will explicitly state this limitation : )

Comment by Paul Gribelyuk [ 16/Jan/14 ]

Thanks Kay. The documentation you linked doesn't state this explicitly, perhaps it should?

Also, I don't need to determine the index of the inner_k2 array, I just want to add an element to it... not a field. Thus, going from a inner_k2 list of 1 item to a list of 2 items. Perhaps the confusion arises from mentining the $set operator, I think I might need to use $push instead.

Comment by Kay Kim (Inactive) [ 16/Jan/14 ]

Hi Paul –
unfortunately the $ positional operator does not work with nested arrays to determine the array index of the element to update; that is, although you can use the positional operator to find the k2 index, currently, you cannot use the positional operator to get the inner_k2 index. That is, you can do

db.foo.update( { "k2.inner_k1": 1 } , { $set: { "k2.$.inner_k2.0.myfield" : 3 }  } )

but, not for the elements of inner_k2.

For more info on the positional operator, http://docs.mongodb.org/manual/reference/operator/update/positional/
I believe that a future release will include the ability to update elements in nested arrays w/o knowing the index.

Comment by Paul Gribelyuk [ 16/Jan/14 ]

Hi Kay,
Thanks. That would almost work if I knew the indices for k2 and inner_k2. In your example you specified them as 0. I am trying to add another hash to the inner_k2 array, so I would like to have a query which creates a document which looks like this:

> { k1 : v1,
> k2 : [
> {inner_k1: inner_v1,
> inner_k2: [
>

{inner_inner_k1: inner_inner_v1, > inner_inner_k2: inner_inner_v2 > }

,
>

{inner_inner_k1: inner_inner_v3, > inner_inner_k2: inner_inner_v4 > }

> ]
> }
> ]
> }

Comment by Kay Kim (Inactive) [ 16/Jan/14 ]

Hi Paul –

Did you mean:

7> db.foo.find().pretty()
{
	"_id" : ObjectId("52d801cbb3fd424641303cba"),
	"k1" : 0,
	"k2" : [
		{
			"inner_k1" : 1,
			"inner_k2" : [
				{
					"inner_inner_k1" : 1,
					"inner_inner_k2" : 2
				}
			]
		}
	]
}
8> db.foo.update( { k1: 0 } , { $set: { "k2.0.inner_k2.0.myfield": 3 } } )
Update WriteResult({ "ok" : 1, "n" : 1 })
9> db.foo.find().pretty()
{
	"_id" : ObjectId("52d801cbb3fd424641303cba"),
	"k1" : 0,
	"k2" : [
		{
			"inner_k1" : 1,
			"inner_k2" : [
				{
					"inner_inner_k1" : 1,
					"inner_inner_k2" : 2,
					"myfield" : 3
				}
			]
		}
	]
}

You can use the dot notation to access array elements by its position:

http://docs.mongodb.org/manual/core/document/#dot-notation

However, if you don't know the actual position of the element you want to use, I believe currently the {$} positional operator does not work with nested arrays.

Comment by Sam Kleinman (Inactive) [ 07/Jan/14 ]

I've edited the ticket title. I just want to make sure that specific questions get addressed outside of the documentation change process. We'll try and get this scheduled as soon as possible.

Comment by Paul Gribelyuk [ 07/Jan/14 ]

Hi Sam,
On the Jira page, you wrote:
"The DOCS jira project is for issues and improvements in the MongoDB documentation. Questions like this are best addressed..."

Perhaps the MongoDB docs would be improved by describing an answer to my question!

-Paul

Comment by Sam Kleinman (Inactive) [ 07/Jan/14 ]

The DOCS jira project is for issues and improvements in the MongoDB documentation. Questions like this are best addressed to one of our support channels: http://www.mongodb.org/about/support/

Generated at Thu Feb 08 07:43:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.