Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - create and update multiple MongoDB documents in one call

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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  }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...