1

I am trying to update a couple of objects in the MongoDB in nodejs. Here's the problem I'm having:
I have an array of objects kind of looks like this:

[{
   "_id": {
        "$oid": "5ed611265828aa77c978afb4"
    },
    "advert_id": "5ec2e4a8bda562e21b8c5052",
    "isCategory": false,
    "name": "2+1 Ev Taşıma",
    "value": "2200 TL"
},
{
    "_id": "40", // this object is recently added so it doesn't 
                // have an oid it needs to be inserted instead of updating.
    "advert_id": "5ec2e4a8bda562e21b8c5052",
    "isCategory": false,
    "name": "4+1 Ev Taşıma",
    "value": "7000 TL"
},
]

I'm trying to update every one of these objects using collection.updateMany with upsert: true Here's the code I wrote:

mongodb.collection('prices').updateMany({}, {$set: post.prices}, {upsert: true}, (error, result) => {
    if(error) throw error;
    res.json(response);
})

Here's the error:

MongoError: Modifiers operate on fields but we found type array instead. For example: {$mod: {<field>: ...}} not 
{$set: [ { _id: "5ed611265828aa77c978afb4", advert_id: "5ec2e4a8bda562e21b8c5052", isCategory: false, name: "2+1 
Ev Taşıma", value: "2000 TL", isVisible: false } ]}

The problem seems that I'm trying to pass prices array directly into the $set pipe. What's a field type and how can I translate my array into that.

Appreciate the help, had some difficult time searching through the documentation but couldn't find any related chapter. I can really appreciate it if you can also link the documentation's related part in your answer.

Database document:

{
    "_id": {
        "$oid": "5ed611265828aa77c978afb4"
    },
    "advert_id": "5ec2e4a8bda562e21b8c5052",
    "isCategory": false,
    "name": "2+1 Ev Taşıma",
    "value": "2000 TL",
    "isVisible": false
}
2
  • can you please explain some more so i can help you. Like which answer you want & document example . Commented Jul 14, 2020 at 13:09
  • I'd like to update every element in the array I mentioned at the beginning of the question. I want each one of them to be upserted. Commented Jul 14, 2020 at 13:15

1 Answer 1

4

I see a problem here {$set: post.prices}

It should be {$set: {'prices':post.prices}} - You should provide a document to $set

Refer

    db.col.find({
  '_id.$oid': '5ed611265828aa77c978afb4'
},
{
  $set: {
    "advert_id": "5ec2e4a8bda562e21b8c5052",
    "isCategory": false
  },
  {
    upsert: true
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well, the error is not shown now but still, I can't update the document. I updated the question, so you can see how my document is in the database.
What do you want to upset? Can you add the value to be inserted to which field? And type of the fields better sample of prices/post
I updated the array so you can see differences clearly. I have a collection called prices. Also, I have an array that I mentioned in the beginning. I'd like to iterate in the array and if it can be updated (if it has an oid) i want to update it and if it doesn't have an oid I want to insert it.
Query part is clear. Once queried, what do you want to upsert?
In the first code block, there's an array I want to upsert every item of it.

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.