简体   繁体   中英

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.

However, those variables are altered after that if statement thus my code does not work.

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.

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 : { $exists: false }:

The results consist of those documents that do not contain the field 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.

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){});
            }
        })
   }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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