ItGo.me Focus on IT Recommend

Home > node.js - Update and/or add array element properties using req.body via Mongoose?

node.js - Update and/or add array element properties using req.body via Mongoose?

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I have the following document:

{
    "_id" : ObjectId("503b83dfad79cc8d26000004"),
    "pdfs" : [
        {
            "title" : "Test document",
            "pdf_id" : ObjectId("504f6793ce351a595d000004"),
            "created_at" : ISODate("2012-09-11T16:32:19.276Z")
        },
        {
            "title" : "Some other doc",
            "pdf_id" : ObjectId("502bf124b4642341230003f0"),
            "created_at" : ISODate("2012-09-11T11:34:19.276Z")
        }
    ]
}

Now in an incoming form via req.body, I have 2 fields: title and description.

I want to update title and insert description for a specified pdf_id, how do I do that?

So in the end, my document will now look like:

{
    "_id" : ObjectId("503b83dfad79cc8d26000004"),
    "pdfs" : [
        {
            "title" : "This is an UPDATED title",
            "description" : "It has an ALL NEW description",
            "pdf_id" : ObjectId("504f6793ce351a595d000004"),
            "created_at" : ISODate("2012-09-11T16:32:19.276Z")
        },
        {
            "title" : "Some other doc",
            "pdf_id" : ObjectId("502bf124b4642341230003f0"),
            "created_at" : ISODate("2012-09-11T11:34:19.276Z")
        }
    ]
}

Just to be clear, I'm really just looking for the Mongoose update syntax.

Recommend:node.js - How to update array in mongodb using mongoose

good joke", "_id" : ObjectId("56660745f644c2501116acce") }, { "posteruserId" : "5665e6867185d87c1e71dbdc", "postedBy" : "lawrence nwoko", "postterscomment" : "good joke",

node.js mongodb express mongoose
|
  this question
edited Sep 11 '12 at 17:28 asked Sep 11 '12 at 16:47 k00k 4,647 9 42 72

 | 

1 Answers
1

---Accepted---Accepted---Accepted---

You can use the $ positional operator to refer to the matched pdfs array element in your $set:

Model.update(
    { 'pdfs.pdf_id': pdf_id }, 
    { $set: { 
        'pdfs.$.title': title, 
        'pdfs.$.description': description 
    }}, function (err, numAffected) { ... }
);

|
  this answer
answered Sep 11 '12 at 17:29 JohnnyHK 148k 25 302 281 1   In the $set, is there any way to assign those properties dynamically? The user may not supply some, and I'd prefer to not have to specify them explicitly. –  k00k Sep 11 '12 at 17:37      Sure, you'd just need to build up that $set object programmatically and then pass that object in as the second parameter to the update call. Sort of like in this other question: stackoverflow.com/questions/12184626/… –  JohnnyHK Sep 11 '12 at 17:41      Cool, thanks. Something that tripped me up, that might help others: It wouldn't update for me when I just had my array set to mixed []. I had to built out the schema for the array and make sure that the _id of the array element you're trying to update is specified as a type: Schema.Types.ObjectId in your schema. –  k00k Sep 11 '12 at 17:53

 | 

Recommend:node.js - Express and Mongoose with MongoDB

" : 3, "_id" : ObjectId("5396e11c13e345570ae174f1"), "created" : ISODate("2014-06-10T10:42:36.925Z"), "name" : "Project", "projects" : [ ObjectId("5396db4f3458c64f09e23ef8"), ObjectId("53982440de337a4e0b301

------splitte line----------------------------