[英]How to store large amount of messages for users in MongoDB?
I need to run my cron scripts to update/insert (upsert) threads (emails) for each user. 我需要运行cron脚本来为每个用户更新/插入(插入)线程(电子邮件)。 I have collection that is structured such as: 我有一些结构如下的集合:
{
"id" : ObjectId("57d7fc5fd34228c47059"),
"id" : "userid",
"primaryEmail" : "user@mail.com",
"threads_list" : [
]
},
{
"_id" : ObjectId("57d7346a73d128c47059"),
"id" : "uderid",
"primaryEmail" : "user2@mail.com",
"threads_list" : [
]
}
...
I want to store threads into threads_list
. 我想将线程存储到threads_list
。 But I also want to update them if they already exist or just skip it if it hasn't change since last upsert, just like I would create collection for each user and update emails stored inside with updateOne
method that has {upsert:true}
param. 但是我也想更新它们(如果它们已经存在),或者如果它们自上次upsert之后没有变化就跳过它,就像我将为每个用户创建集合并使用具有{upsert:true}
参数的updateOne
方法更新存储在其中的电子邮件一样。
I don't mind changing threads_list
attribute into object type if necessary. 我不介意在必要时将threads_list
属性更改为对象类型。
How is that done properly? 如何正确完成?
Try this code 试试这个代码
db.collection('users').update(
{ _id:user.id,
$set: {'primaryEmail': newEmail}
},
{ $addToSet: { 'threads_list': { $each: resp.labels } } }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.