Kamilski81
Kamilski81

Reputation: 15137

How do i do a findandmodify on an embedded document, in mongodb?

I can't seem to get this to work. Is there a better possibility or option instead of a findandmodify in this case?

db.runCommand(
                {
                  findAndModify: "articles",
                  query: {"comments._id": ObjectId("515221650be1684cb9000002")},
                  new: true,
                  update: {"$push":{"comments.votes.up":"511cff226d6e0d1f20000001"}, "$inc":{"comments.votes.count":1, "comments.votes.up_count":1, "comments.votes.point":1}}
                }
             )

Upvotes: 1

Views: 702

Answers (1)

Devesh
Devesh

Reputation: 4560

Is comment is subdocument collection ? if yes then is it that you are missing $ sign. like comments.$.votes.up . Try out this one . It will work

       db.runCommand(
            {
              findAndModify: "articles",
              query: {"comments._id": ObjectId("515221650be1684cb9000002")},
              new: true,
              update: {"$push":{"comments.$.votes.up":"511cff226d6e0d1f20000001"}, "$inc":{"comments.$.votes.count":1, "comments.$.votes.up_count":1, "comments.$.votes.point":1}}
            }
         )

Upvotes: 2

Related Questions