简体   繁体   English

MongoDB和C#-检查文档是否存在,是否不插入

[英]MongoDB and C# - Checking if document exists, if it doesn't insert it

I currently get a list of 50 restaurants from the Yelp Fusion API. 目前,我从Yelp Fusion API中获得了50家餐厅的列表。 I want to save these restaurants in my database for further use in my application. 我想将这些餐厅保存在数据库中,以备将来在应用程序中使用。

Since I get this list based on the user's coordinates, it's possible the API will return restaurants that are already in my database. 由于我是根据用户的坐标获取此列表的,因此该API可能会返回数据库中已有的餐厅。 To prevent duplicates, I check if the restaurant ID provided by yelp already exists in the database. 为防止重复,我检查yelp提供的餐厅ID是否已存在于数据库中。 If it's already in my database, it won't insert that restaurant. 如果已经在我的数据库中,则不会插入该餐厅。

I'm using a MongoDB database. 我正在使用MongoDB数据库。

I accomplish this using the following function: 我使用以下功能完成此操作:

public async static void CheckSubjects(List<Subject> subjects)
    {
        SubjectRepository rep = new SubjectRepository();
        List<Subject> insert = new List<Subject>();
        foreach (var item in subjects)
        {
            if (!rep.SubjectsExists(item).GetAwaiter().GetResult())
                insert.Add(item);
        }
        if (insert.Count > 0)
            await rep.CreateAllSubjects(insert);
    }

In this function a subject is a restaurant. 在此功能中,主题是餐厅。 The repository is the class which communicates with the database. 存储库是与数据库进行通信的类。

This is my function that checks if the restaurant already exists: 这是我检查餐厅是否已经存在的功能:

public async Task<bool> SubjectExists(Subject sub)
    {
        var result = await db.Subjects.Find(Builders<Subject>.Filter.Eq("yelp_id", sub.yelp_id)).FirstOrDefaultAsync();
        if (result != null)
            return true;
        else
            return false;
    }

You can see it's very performance heavy to make 50 calls to the database to see if it already exists. 您可以看到对数据库进行50次调用以查看其是否已经存在,这对性能非常重要。 I did some research and found the upsert function from MongoDB. 我做了一些研究,发现MongoDB提供了upsert功能。 I just don't know how I could use this in my scenario. 我只是不知道如何在我的场景中使用它。

Could anyone help me out with this? 有人可以帮我吗? Thanks in advance! 提前致谢!

When using upsert as an option during an update, if a document doesn't exist, it will be created. 在更新过程中使用upsert作为选项时,如果不存在文档,则会创建该文档。 Instead of trying to find each restaurant ans then insert them, you could call an update for each of them with upsert set as true (I think it'd be something like this ) 而不是试图找到那么每个餐厅ANS插入它们,你可以调用一个更新为他们每个人UPSERT设置为真(我觉得这是像这样

Another aproach that could be even more efficient would be setting yelp_id as a key by creating an unique index , then when inserting duplicated entries it would fail, so you wouldn't need to worry if it is already in the database. 另一个可能更有效的方法是通过创建唯一索引yelp_id设置为键,然后在插入重复的条目时它将失败,因此您不必担心它是否已在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM