create and update multiple MongoDB documents in on

2019-01-12 12:45发布

问题:

Let's imagine that I have a MongoDB collection like so:

[
{someId: 1, someProp: prop1, isOpen: true},
{someId: 2, someProp: prop1, isOpen: true},
{someId: 3, someProp: prop1, isOpen: true},
{someId: 4, someProp: prop1, isOpen: true},
]

I want to make a call to it with an array of objects, but some of these objects will be new, and some will need to override existing documents.

So let's say my array looks like this:

[
{someId: 1, someProp: prop1, isOpen: true},
{someId: 2, someProp: prop1, isOpen: true},
{someId: 5, someProp: prop1, isOpen: true},
{someId: 6, someProp: prop1, isOpen: true},
]

How do I write a MongoDB query that does the following.

If someId matches an object in my second array, just change the isOpen to false. If someId doesn't match any objects then insert them into collection.

So after I run my query i would end up with:

[
{someId: 1, someProp: prop1, isOpen: false},
{someId: 2, someProp: prop1, isOpen: false},
{someId: 3, someProp: prop1, isOpen: true},
{someId: 4, someProp: prop1, isOpen: true},
{someId: 5, someProp: prop1, isOpen: true},
{someId: 6, someProp: prop1, isOpen: true},
]

Is it a good idea to call a forEach() and just use:

db.books.update(
   { item: "ZZZ135" },
   {
     item: "ZZZ135",
     stock: 5,
     tags: [ "database" ]
   },
   { upsert: true }
)

...in the loop? Probably not. That would mean multiple calls to the db. Is there a more efficient way of doing that?

回答1:

Basically you need bulkWrite operation with upsert

const array = [
    {  someId: 1, someProp: prop1, isOpen: true  },
    {  someId: 2, someProp: prop1, isOpen: true  },
    {  someId: 5, someProp: prop1, isOpen: true  },
    {  someId: 6, someProp: prop1, isOpen: true  }
]


Model.bulkWrite(
  array.map((data) => 
    ({
      updateOne: {
        filter: { someId: data.someId },
        update: { $set: { isOpen: false, someProp: data.someProp } },
        upsert: true
      }
    })
  )
})

Will give you the output

[
  {  someId: 1, someProp: prop1, isOpen: false  },
  {  someId: 2, someProp: prop1, isOpen: false  },
  {  someId: 3, someProp: prop1, isOpen: true  },
  {  someId: 4, someProp: prop1, isOpen: true  },
  {  someId: 5, someProp: prop1, isOpen: true  },
  {  someId: 6, someProp: prop1, isOpen: true  }
]