Ruby Mongodb查询-检查现有文档
是否可以在Ruby中编写代码,以便在我的数据库中已经存在一个文档时将新文档和已经存在的文档进行聚合,如果不存在,则将新文档添加到数据库中 我知道如何进行聚合,但在这种情况下我不知道如何使用$exists 为了更新现有文档,我创建了,但如果我想推送整个哈希,我想我不能使用它:Ruby Mongodb查询-检查现有文档,ruby,mongodb,mongodb-query,Ruby,Mongodb,Mongodb Query,是否可以在Ruby中编写代码,以便在我的数据库中已经存在一个文档时将新文档和已经存在的文档进行聚合,如果不存在,则将新文档添加到数据库中 我知道如何进行聚合,但在这种情况下我不知道如何使用$exists 为了更新现有文档,我创建了,但如果我想推送整个哈希,我想我不能使用它: db.coll.update({"title" : "Document"}, {$push:{"comments":{ "user":'user2', "message": 'My second com
db.coll.update({"title" : "Document"},
{$push:{"comments":{
"user":'user2',
"message": 'My second comment',
"dateCreated": new Date(2013,11,10,2,35),
"like": 0
}}})
在更新查询中使用
upsert:true
选项
从mongodb手册:
Upsert:如果设置为true,则在没有文档与查询条件匹配时创建新文档。默认值为false,当未找到匹配项时,不会插入新文档
您的查询可能如下所示:
db.coll.update({"title" : "Document"}, {$push: {
"comments":{
"user":'user2',
"message": 'My second comment',
"dateCreated": new Date(2013,11,10,2,35),
"like": 0
}
},
{upsert: true}
})