[英]How to insert documents if they don't already exist from within a remote method in MongoDb using LoopBack
I'm very new to MongoDB (about 4 days in) and I'm trying to insert documents into my collection from within a remote method using Loopback without adding duplicate documents. 我是MongoDB的新手(大约4天之内),我试图使用Loopback从远程方法中将文档插入到我的集合中,而无需添加重复的文档。
I firstly tested adding documents as such: 我首先测试了这样添加文档:
Events.create(resultData);
Which worked without issue. 哪个没有问题。
I then moved on to trying to add a document without adding duplicates a few other answers : 然后,我继续尝试添加文档而不添加重复的其他一些答案 :
Events.update(data,data,{upsert: true});
However, this did not add anything to the database. 但是,这没有向数据库添加任何内容。
I decided to move on and try and see if I could first check to see if a document could be found from a collection and therefore not add the document. 我决定继续尝试,看看是否可以先检查是否可以从集合中找到文档,因此不添加该文档。 Similar to this answer . 类似于这个答案 。
if(Events.find({itemOfData: resultData.itemOfData},{limit: 1}).size < 1){
Events.create(resultData);
}
However, as before it doens't create any documents. 但是,和以前一样,它不会创建任何文档。
I'm not sure what to try next or whether my implementation of the above solutions is faulty. 我不确定下一步该怎么做,或者我对上述解决方案的执行是否有问题。
Events.update(data,data,{upsert: true});
LoopBack models don't expose MongoDB API. LoopBack模型不公开MongoDB API。 Methods like create
are providing database-agnostic API that's mapped by connectors to database commands. 诸如create
类的方法提供了与数据库无关的API,该API由连接器映射到数据库命令。
If you want to insert a document only if it does not exist, you can use one of the following methods (depending on what you are trying to achieve): 如果只想插入不存在的文档,则可以使用以下方法之一(取决于要实现的目标):
replaceOrCreate
patchOrCreate
(also known as upsert
and updateOrCreate
) patchOrCreate
(也称为upsert
和updateOrCreate
) findOrCreate
upsertWithWhere
if(Events.find({itemOfData: resultData.itemOfData},{limit: 1}).size < 1){
The find
method accepts a Filter object (see Querying data ) which contains not only the condition for matching records, but also things like pagination and what related models to include. find
方法接受一个Filter对象(请参阅Querying data ),该对象不仅包含匹配记录的条件,而且还包含诸如分页和要包含的相关模型之类的内容。 The condition is stored in where
property of the Filter object. 条件存储在Filter对象的where
属性中。
Also the find
method returns a Promise, you need to wait until it's resolved before counting the number of returned records. 同样, find
方法返回一个Promise,您需要等待它解决后才能计算返回的记录数。
const found = Events.find({
where: {itemOfData: resultData.itemOfData}
limit: 1
});
if (found.size < 1){
// ...
}
While the solution based on find
may seem to work well in development, it introduces a race condition where two "same" records can be created when the application is under high load. 尽管基于find
的解决方案在开发中似乎可以很好地工作,但是它引入了竞争条件,当应用程序处于高负载时,可以创建两个“相同”记录。 Whenever possible, it's advised to use built-in functions like patchOrCreate
that use database-specific means to guarantee atomicity. 建议尽可能使用内置函数,例如patchOrCreate
,这些函数使用特定于数据库的方式来保证原子性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.