gpt4 book ai didi

node.js - 如何使用node js更新mongodb集合中所有文档的_id?

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:14 25 4
gpt4 key购买 nike

我有一个要求,我想要更新所有文档的 id 值。我知道如何仅更新集合中的一个文档。但我怎样才能更新所有文档呢?

db name - mydb
collection name - customers
documents - many....

/* 1 */
{
"_id" : ObjectId("5e1d814ac488402bcf1600b3"),
"name" : "Company Inc",
"address" : "Highway 37"
}

/* 2 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328384"),
"name" : "Company Inc 10",
"address" : "Highway 37"
}

/* 3 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328385"),
"name" : "Company Inc 11",
"address" : "Highway 37"
}

/* 4 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328386"),
"name" : "Company Inc 12",
"address" : "Highway 37"
}

/* 5 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328387"),
"name" : "Company Inc 13",
"address" : "Highway 37"
}
etc.,

在这里我只想更新所有文档中的 _id 值。对于每个文档,我需要更新 id 作为数组中的给定输入。我只是简单地尝试将 i++ 作为值传递。它不起作用。

var mongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

mongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var collection = dbo.collection("customers");
var customers = collection.find().toArray();
var bulkArray = [];
customers.forEach(function* (d, i) {
bulkArray.push({ updateOne: { filter: { _id: mongodb.ObjectID(d._id) },
update: { $set: { _id: i++ }}, upsert:true }});
});
collection.bulkWrite(bulkArray, {ordered:true, w:1});
});

最佳答案

你可以试试这个:

db.customers.aggregate([{ $group: { _id: '', data: { $push: '$$ROOT' } } }, {
$project: {
data: {
$map:
{
input: "$data",
as: "each",
in: { $mergeObjects: ['$$each', { '_id': { $indexOfArray: ['$data', '$$each'] } }] }
}
}
}
}, { $unwind: '$data' }, { $replaceRoot: { newRoot: '$data' } }, { $out: "customersNew" }])

从客户收集数据:

/* 1 */
{
"_id" : ObjectId("5e1d814ac488402bcf1600b3"),
"name" : "Company Inc",
"address" : "Highway 37"
}

/* 2 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328384"),
"name" : "Company Inc 10",
"address" : "Highway 37"
}

/* 3 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328385"),
"name" : "Company Inc 11",
"address" : "Highway 37"
}

/* 4 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328386"),
"name" : "Company Inc 12",
"address" : "Highway 37"
}

/* 5 */
{
"_id" : ObjectId("5e1d9176bd5be834bf328387"),
"name" : "Company Inc 13",
"address" : "Highway 37"
}

写入客户的结果新集合:

/* 1 */
{
"_id" : 0,
"name" : "Company Inc",
"address" : "Highway 37"
}

/* 2 */
{
"_id" : 1,
"name" : "Company Inc 10",
"address" : "Highway 37"
}

/* 3 */
{
"_id" : 2,
"name" : "Company Inc 11",
"address" : "Highway 37"
}

/* 4 */
{
"_id" : 3,
"name" : "Company Inc 12",
"address" : "Highway 37"
}

/* 5 */
{
"_id" : 4,
"name" : "Company Inc 13",
"address" : "Highway 37"
}

不太清楚为什么需要这样做。

  1. 无论如何,您无法更新现有文档的_id,除非您删除该文档并将其插入新的所需 _id

  2. 不是从数据库读取所有文档,而是删除和重写 所有返回到集合将是对数据库的两种或三种方式调用& 对于如此大量的数据,这也可能令人担忧。

  3. 您可以尝试此查询,但在最后阶段要小心 $out - 它 会将聚合输出写入指定的集合名称(不要指定现有集合的名称 - 除非需要/测试)! 您可以通过删除 $out 阶段来验证正在写入的数据,然后使用 $out 将数据写入新集合,一旦您熟悉了新集合中的数据 集合删除旧集合并将新集合重命名为 (customersNew) 客户

  4. 如果此聚合未能达到内存限制,您可能需要使用 allowDiskUse:true aggregate

关于node.js - 如何使用node js更新mongodb集合中所有文档的_id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59736853/

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