1

I need to update the document's array element in mongodb, I have multiple ids of that array. I need to update the matching element in that array.

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

Start date should be updated for the matching plan id in a single database query.

1 Answer 1

2

i think your best bet is to use a bulkWrite command such as this:

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

i can't help you with the js/node driver/client code but here's c# code that generated the above command in case anyone's interested:

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();
        }
    }
}

Sign up to request clarification or add additional context in comments.

Comments

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.