[英]How to insert one million notification records for one million user records in MongoDB efficiently using the best practices?
我有两个 collections 用户和通知。 我有 100 万用户,我想为每个用户创建一条通知记录,所以我需要插入 100 万条通知记录。 使用 MongoDB 和 nodejs 的最佳实践是什么? 我应该手动遍历用户还是 MongoDB 中是否有内置实用程序来有效地这样做。
Users
架构:
{
"_id": "61b76dcef2d79f189f619daa",
"email": "test@test.com",
"name": "test"
}
Notifications
架构:
{
"_id": "61b76dcef2d79f189f610000",
"userID": "61b76dcef2d79f189f619daa", //this is ref to user
"data": "test data",
"isSeen": false
}
这是使用一些“种子”数据创建notifications
集合的一种方法。
【MongoDB Atlas《导出管道到节点》】
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
/*
* Requires the MongoDB Node.js Driver
* https://mongodb.github.io/node-mongodb-native
*/
const agg = [
{
'$project': {
'userId': '$_id',
'data': 'test data',
'isSeen': {
'$not': true
}
}
}, {
'$out': 'notifications'
}
];
MongoClient.connect(
'connection string',
{ useNewUrlParser: true, useUnifiedTopology: true },
function(connectErr, client) {
assert.equal(null, connectErr);
const coll = client.db('yourDatabase').collection('users');
coll.aggregate(agg, (cmdErr, result) => {
assert.equal(null, cmdErr);
});
client.close();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.