0

How do i query any objects whos array contains a player with a matching case-insensitive name? db.collection.find(players.name: search)

This is a example object of how the data is stored (I've removed all irrelevant fields to the question).

{ "_id" : ObjectId("5d67f29ae6504b451c3aca3e"), "players" : [
 { "name" : "Jenny"}, { "name" : "Benny"}, { "name" : "Kenny" } ] }

One solution would be to just iterate through all documents in the collection and then in turn iterate through all names in each document but I'm trying to avoid this. Another is perhaps to map names to objectIds?

2

3 Answers 3

1

You can use a regex to do this:

db.collection.find("players.name": {'$regex' : '^string$', '$options' : 'i'})

Sign up to request clarification or add additional context in comments.

Comments

0

Here's my, apparently more traditional, take on it. I didn't know a construct like your own answer here uses exists in the language, I have to do some reading. Anyway, here we go.

function find_player( data, player_name )
{
    return data.filter( (o) => {
        return o.players.find( (player) => {
            return player.name == player_name;
        });
    });
}

let objects = [
    { "_id" : "foo", "players" : [ { "name" : "Jenny"}, { "name" : "Benny"}, { "name" : "Kenny" } ] },
    { "_id" : "bar", "players" : [ { "name" : "Johnny"}, { "name" : "Bunny"}, { "name" : "Kinny" } ] }
];

let found = find_player(objects, "Kinny");

console.log( found[0]._id ); // bar

Comments

0

Thanks $elemMatch was indeed the solution.

dbo.collection('replays').find({ players: {
        $elemMatch: {name : {$regex: new RegExp('^'+ search + '$', "i") }} 
    }
}).toArray(function (err, res) {
    console.log(res);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.