0

Let me start by saying I'm sorry if this has been answered, but I can't get other questions on this site to fit my needs and, more importantly, work.

I have the below example document, with a subdocument of 'address':

{ 
    "_id" : ObjectId("....")
    ,"addresses" : 
        [{
            "start" : ISODate("1973-07-10T00:11:51.111Z")
            ,"value" : "123 long road"
        }] 
}

What I need to do is to close the existing address record with an end attribute, and add a new line for the new address with a new start and value attribute. Eventually, I'll need to do this again so the code needs to update the subdocument record where end does not exist.

The below code does not work, but it's about as far as I can get:

db.sites.update(
      {"_id" : ObjectId("....")
        , "addresses.end" : {"$exists" : false}}
     ,{"$set": {"addresses.$.end" : "fdsa"}});

This gives the error:

Cannot apply the positional operator without a corresponding query field containing an array.

Can anyone point me in the right direction?

3 Answers 3

2

Juste replace in your query "addresses.end" : {"$exists" : false} with:

addresses: {$elemMatch: {end: {$exists: false}}}
Sign up to request clarification or add additional context in comments.

4 Comments

Brilliant, the lot of you. Thanks very much :)
Guys, it seems that using addresses.$.end in conjunction with $elemMatch only ever updates the first subdocument, and not the one which was a missing end attribute. any ideas how to always update the subdocument that doesn't have an end attribute?
It works for me. It updates the first subdocument with a missing end attribute... Are you sure you do not have several subdocuments with a missing end attribute?
Got it working too, I had another condition that was never true. Thanks again.
0

Your address field is poorly defined. you need make it a subdocument or an array of subdocuments. ie { "_id" : ObjectId("....") ,"addresses" : [ { "start" : ISODate("1973-07-10T00:11:51.111Z") ,"value" : "123 long road" } ] }

your query should then work!

2 Comments

Sorry, I missed out the curlies {} - they are there really, just hiding in my above post. I'll make an edit.
@Barno is there a workaround doing multiple updates?
0

I think that the Query should be more specific

**Updated **

db.sites.update ( {"_id" : ObjectId("...."), addresses: { "$elemMatch" : { end:{$exists : false}} } }, {"$set": {"addresses.$.end" : "fdsa"}});

db.sites.find()

results:

{
    "_id" : ObjectId("53df93da560b7815e1237934"),
    "addresses" : [ 
        {
            "start" : ISODate("1973-07-10T00:11:51.111Z"),
            "value" : "123 long road",
            "end" : "fdsa"
        }
    ]
}

but you can update only one

Take a look http://docs.mongodb.org/manual/reference/operator/projection/positional/#proj.S

You can t update more element in Array https://jira.mongodb.org/browse/SERVER-1243

1 Comment

This is not what the OP is asking: he wants to update the element that does not have and "end" attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.