我有一个ID数组,如果没有,则需要更新对象文档mongodb中数组中的匹配对象,然后插入

时间:2019-08-02 13:12:32

标签: javascript node.js mongodb

  

我需要在mongodb中更新文档的数组元素,我有   该数组的多个ID。我需要更新中的匹配元素   该数组。

data= [
   {
    "planId" : "plan_FVNvrmjWT7fRDY", 
    "startTime": ISODate("2019-07-30T07:05:23.000Z")
   },
   {
    "planId" : "plan_FVNvCvm7Qg5uYB", 
    "startTime": ISODate("2019-07-30T07:05:23.000Z"),
   },
   {
    "planId" : "plan_FVNvrmjWT7fRAS", 
    "startTime": ISODate("2019-07-30T07:05:41.000Z"),
   }
   ]

   document = { 
   "_id" : ObjectId("5d3fec3982d76f26f34afdc5"),
   "customerId" : ObjectId("5d383013647a3c42835fd7e6"),
   "__v" : 0,
   "createdAt" : ISODate("2019-07-30T07:05:29.986Z"),
   "subscriptions" : [ 
       {
           "_id" : ObjectId("5d3fec39c81f463a257862d0"),
           "planId" : "plan_FVNvrmjWT7fRDY",
           "startDate" : ISODate("2019-07-30T07:05:23.000Z"),
           "endDate" : ISODate("2019-08-30T07:05:23.000Z"),
           "status" : "Active"
       }, 
       {
           "_id" : ObjectId("5d3fec39c81f463a257862cf"),
           "planId" : "plan_FVNvCvm7Qg5uYB",
           "startDate" : ISODate("2019-07-30T07:05:23.000Z"),
           "endDate" : ISODate("2019-08-30T07:05:23.000Z"),
           "status" : "Active"
       }, 
       {
           "_id" : ObjectId("5d3fec4bc81f463a257862d2"),
           "planId" : "plan_FVNvrmjWT7fRAS",
           "startDate" : ISODate("2019-07-30T07:05:41.000Z"),
           "endDate" : ISODate("2019-08-30T07:05:41.000Z"),
           "status" : "Active"
       }
     ]
}

应该在单个数据库查询中为匹配的计划ID更新开始日期。

1 个答案:

答案 0 :(得分:1)

我认为最好的选择是使用bulkWrite这样的命令:

db.collection.bulkWrite([
{ updateOne: {
        filter: {
            "subscriptions": {
                "$elemMatch": {
                    "planId": "plan_FVNvrmjWT7fRAS" } } },
        update: {
            "$set": {
                "subscriptions.$.startDate": ISODate("2019-08-02T15:06:06.783Z")
            } } } }, 
{ updateOne: {
        filter: {
            "subscriptions": {
                "$elemMatch": {
                    "planId": "plan_FVNvCvm7Qg5uYB"
                } } },
        update: {
            "$set": {
                "subscriptions.$.startDate": ISODate("2019-08-02T15:06:06.783Z")
            } } } }, 
{ updateOne: {
        filter: {
            "subscriptions": {
                "$elemMatch": {
                    "planId": "plan_FVNvrmjWT7fRDY"
                } } },
        update: {
            "$set": {
                "subscriptions.$.startDate": ISODate("2019-08-02T15:06:06.783Z")
            } } } }])

我无法为您提供js / node驱动程序/客户端代码,但是以下c#代码会在遇到任何人感兴趣的情况下生成上述命令:

using MongoDB.Driver;
using MongoDB.Entities;
using System;

namespace StackOverflow
{
    public class Document : Entity
    {
        public Subscription[] subscriptions { get; set; }
    }

    public class Subscription
    {
        public string planId { get; set; }
        public DateTime startDate { get; set; }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            new DB("test");

            var data = new[] {
                new Subscription {
                    planId = "plan_FVNvrmjWT7fRDY",
                    startDate = DateTime.UtcNow
                },
                new Subscription {
                    planId = "plan_FVNvCvm7Qg5uYB",
                    startDate = DateTime.UtcNow
                },
                new Subscription {
                    planId = "plan_FVNvrmjWT7fRAS",
                    startDate = DateTime.UtcNow
                }
            };

            var bulk = DB.Update<Document>();

            foreach (var sub in data)
            {
                bulk.Match(b => b.ElemMatch(d => d.subscriptions, s => s.planId == sub.planId))
                    .Modify(b => b.Set(d => d.subscriptions[-1].startDate, sub.startDate))
                    .AddToQueue();

            }

            bulk.Execute();
        }
    }
}