3

I have a method which inserts a new document into the collection "items".

//Example data of variables
var home = "home1";
var itemId = "123";
var username = Meteor.user().username;

//Upsert command
items.upsert({
    '_id': home
}, {
    $push: {
        'createdItems': {itemId, username, home}

    }

});

This creates a document like this:

"_id" : "home1",
    "createdItems" : [ 
        {
            "itemId" : "123",
            "username" : "Jon Doe",
            "home" : "home1"
        }, 
        {
            "itemId" : "456",
            "username" : "Jon Doe2",
            "home" : "home1"
        }, 
        {
            "itemId" : "789",
            "username" : "Jon Doe3",
            "home" : "home1"
        }
    ]
}

Now I need to be able to update the existing fields and insert new fields into these objects later. The itemId within these objects is the "reference". So I need a mongoDB function which inserts or updates fields by using the "itemId" as a query operator. Something like this:

//update a field

    items.upsert({
    'itemId': "123"
    }, {

        $set: {
    'createdItems': {username: "Clark Kent"}
                }

        });

//insert new field

    items.upsert({
    'itemId': "123"
    }, {

        $set: {
    'createdItems': {Value: 1000}
                }

        });

Or do I have to use $push? These commands are not delivering the results that I need. At the end the document should look like this:

"_id" : "home1",
    "createdItems" : [ 
        {
            "itemId" : "123",
            "username" : "Clark Kent",
            "home" : "home1",
            "Value" : 1000
        }, 
        {
            "itemId" : "456",
            "username" : "Jon Doe2",
            "home" : "home1"
        }, 
        {
            "itemId" : "789",
            "username" : "Jon Doe3",
            "home" : "home1"
        }
    ]
}

Or do I have to use another data schema for the collection? If yes, which one can I choose when I want to have "itemId, "username" and "home" still in an array but "packed together"?

Many thanks.

2
  • 1
    Can you check if this query will work? items.update({ 'createdItems.itemId': '123' }, { $set: { 'createdItems.$.username': 'Clark Kent' }}) Commented Jul 23, 2017 at 10:50
  • Nice one! Works perfectly with "update", many thanks! Do you also know whether it is possible to "insert/upsert" a new field into the object? Commented Jul 23, 2017 at 11:14

1 Answer 1

9

If you want to update object in array query below will work fine

items.update({ 
  'createdItems.itemId': '123' 
}, { 
  $set: { 
    'createdItems.$.username': 'Clark Kent' 
  }
})

You can also easily add new fields to the object using the same query.

items.update({ 
  'createdItems.itemId': '123' 
}, { 
  $set: { 
    'createdItems.$.username': 'Clark Kent' 
    'createdItems.$.newField': 'newFieldValue' 
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Hey I am trying to set a new field using this query "tasks.$.finishDate": Date. now but it is not inserting to my array, with this query I am only able to update fields, Am I doing something wrong?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.