简体   繁体   English

如何检查具有某些字段的文档是否已存在于 MongoDB 数据库中?

[英]How to check if document with certain fields already exists in the MongoDB database?

I'm trying to check whether a document with certain fields already exist in the database.我正在尝试检查数据库中是否已存在具有某些字段的文档。

I have a player object and I wanna make sure that there isn't another object with the same name and surname already in the database.我有一个玩家对象,我想确保数据库中没有另一个具有相同名称和姓氏的对象。 I also want to check that the team name does not already occur.我还想检查团队名称是否已经出现。

The code below does not work because the code in the callback functions get executed after the if statement below them where the decision is made whether the data should be saved in the database based on the value of two variables that are supposed to get altered in the callback function if the certain condition is met.下面的代码不起作用,因为回调函数中的代码在它们下面的 if 语句之后执行,其中决定是否应根据两个变量的值将数据保存在数据库中满足一定条件的回调函数。

However, those variables are altered after that if statement thus my code does not work.但是,这些变量在 if 语句之后被更改,因此我的代码不起作用。

The question is.问题是。 How do I make the callback function execute when I want it to.如何让回调函数在我想要的时候执行。 Or is there a better way to do this kind of validation?或者有没有更好的方法来进行这种验证?

This is my code:这是我的代码:

let team_exists = false;
let players_exist = false;

Team.count({name: team.name}, function(err, count){
    if (count>0){
        team_exists = true;
    }
});

players.forEach((function(player){
    Player.count({name: player.name, surname: player.surname}, function(err, count){
        if (count>0){
            players_exist = true;
        }
    })
}))

if(!team_exists && !players_exist){
    Player.insertMany(players, function(err){
        if(err){
            cosole.log(err);
        }
    })

    team.save(function(err){});
}

There is a mongodb operator $exists to check if a field exists in a document.有一个 mongodb 运算符$exists来检查文档中是否存在字段。

Usage用法

The following examples uses a collection named records with the following documents以下示例使用名为记录的集合与以下文档

{ a: 5, b: 5, c: null }
{ a: 3, b: 7, c: 8 }
{ a: 9, c: 9 }
{ a: 1, b: 2, c: 3 }
{ a: 2, c: 5 }
{ a: 3, b: 2 }

The following can be used to check all the documents where b does not exist下面可以用来检查b不存在的所有文件

b : { $exists: false }: b : { $exists: false }:

The results consist of those documents that do not contain the field b:结果由那些不包含字段 b 的文档组成:

{ a: 9, c: 9 }
{ a: 2, c: 5 }

In your case, check if a document aleready exists in the collection using the above mentioned query.在您的情况下,使用上述查询检查集合中是否存在文档。 Then you can insert documents conditionally after the results are returned.然后可以在返回结果后有条件地插入文档。

Once possible solution is to place conditional code inside the callback.一种可能的解决方案是在回调中放置条件代码。 This would allow you to process this without needing the booleans at all这将允许您在完全不需要布尔值的情况下进行处理

Building an $or predicate from the players list allows you to check players_exist with a single query to the database instead of a query per player.从玩家列表构建$or谓词允许您通过对数据库的单个查询而不是每个玩家的查询来检查玩家存在。

Team.count({name: team.name}, function(err, count){
    if (count == 0){
        let playerquery = [];
        players.forEach(function(player){ 
           playerquery.push({ name: player.name, 
                              surname: player.surname}); 
        })
        Player.count({$or:playerquery}, function(err, count){
            if (count == 0){
                Player.insertMany(players, function(err){
                        if(err) {
                            console.log(err);
                        }
                })
                team.save(function(err){});
            }
        })
   }
})

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

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