gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:22:15 26 4
gpt4 key购买 nike

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

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

最佳答案

我认为最好的办法是使用像这样的 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();
}
}
}

关于javascript - 我有一个 id 数组,需要更新对象文档 mongodb 中数组中的匹配对象,如果没有则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com