简体   繁体   English

如果使用LoopBack从MongoDb的远程方法中不存在文档,如何插入文档

[英]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 (也称为upsertupdateOrCreate
  • 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.

相关问题 环回:存储来自远程方法的对象,以便以后在服务器内重用 - Loopback: Store object from remote method for later reuse within the Server 使用 loopback-connector-remote 在另一个环回服务中调用自定义方法不会创建正确的 URL - Using loopback-connector-remote to call custom method in another loopback service doesn't create correct URL 在回送中查询mongodb文档 - Query mongodb documents in loopback 如果文档不存在,如何基于 firestore 中的时间戳创建文档? - How to create documents if they don't exist based on a timsteamp in firestore? 从 Remote 方法 Loopback 返回的 PDF 已损坏 - PDF returned from Remote method Loopback is corrupted 在环回中过滤远程方法 - Filter Remote Method in loopback 如何使用 id mongodb 在嵌入式文档数组中查找数据? - How to find data within embedded documents array using id mongodb? 远程方法未在回送API资源管理器中显示 - Remote method doesn't show up in loopback API explorer 我想在角对象中使用lodash将不存在的项目推入已声明的对象中,如果它们尚不存在 - I'd like to push non-existing items inside an already declared object if they don't already exist using lodash in angular 如何在环回远程方法中将 pdf 文件作为 res 发送? - How to send the pdf file as res in loopback remote method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM