1

Can't seem to figure out how to update a single field.

Whatever I try, to update one field (picture: "url") the rest of the fields are being removed and set to null.

I use the following command now:

Meteor.users.update(
          userId,
          {
            $set: {
              profile: {
                picture: fileURL
              }
            }
          }
        );

And this is what happens with the rest of the object after update.

{
  "data": {
    "user": {
      "_id": "J222CqhR777GcXD",
      "createdAt": "Tue Sep 11 2018 19:23:55 GMT-0400 (EDT)",
      "email": "scott@example.com",
      "profile": {
        "about": null,
        "firstName": null,
        "lastName": null,
        "phone": null,
        "picture": "https://image.path/source.jpg"
      }
    }
  }
}

Is there another operator I am missing or some other way to do this?

In another document I am querying for all the data before, storing it in a React state and then passing all the fields that I have not changed to the update, but that seems rather inefficient and unnecessary step(???)

Is there really not a way to update just a single field without affecting the rest of the document?

cheers guys

1

Had to rewrite like so (profile.picture in one line with dot notation), this works:

Meteor.users.update(
    userId,
    {
        $set: {
            'profile.picture': fileURL
        }
    }
);

Answered by @coagmano on meteor forums

1
  • To specify a in an embedded document or in an array, use dot notation. As you have used here like 'profile.picture': fileURL. Sep 13 '18 at 10:52
1

As per mongodb documentation here The $set operator replaces the value of a field with the specified value.

{ $set: { <field1>: <value1>, ... } }

To specify a in an embedded document or in an array, use dot notation.

Dot Notation

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.

For Arrays

To specify or access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:

"<array>.<index>"

For example, given the following field in a document:

{
   ...
   contribs: [ "Turing machine", "Turing test", "Turingery" ],
   ...
}

For Embedded Documents

To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes:

"<embedded document>.<field>"

For example, given the following field in a document:

{
   ...
   name: { first: "Alan", last: "Turing" },
   contact: { phone: { type: "cell", number: "111-222-3333" } },
   ...
}
  • To specify the field named last in the name field, use the dot notation "name.last".
  • To specify the number in the phone document in the contact field, use the dot notation "contact.phone.number".

Note: The maximum BSON document size is 16 megabytes.

For further your reference here and here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.