user3642365
user3642365

Reputation: 579

MongoDB - querying for multiple objects

I am working on a project using MongoDB (monk module for Node js) where I store sports match data into a collection. Each entry has the following properties:

_id: Number,
team1: Array,
team2: Array,
winner: Number

I want to query the collection as follows: if team1 or team2 contain x, y and z, then I want to return the entry, and hopefully have a list of entries to work with. IE, I want to see the number of matches that x,y,z played on the same team.

Is there any way to do this? I am currently reading the documentation for monk and mongodb, but I'm a noob at nosql databases so it'll take me a while.

Upvotes: 0

Views: 994

Answers (1)

jmdiego
jmdiego

Reputation: 552

Well, the query that you should use is something like:

var myQuery = {$or : [{team1 : {$all : [x, y, z]}}, {team2 : {$all : [x, y, z]}}]}

And this would go inside the find request (taken from the monk gitHub docs):

var db = require('monk')('localhost/mydb');
var users = db.get('users');
users.find(myQuery, function (err, docs){
    // err is null unless something went wrong
    // docs will be an array of all the matching documents
    console.log("Found " + docs.length + " matches");
});

Hope that helps!

Upvotes: 1

Related Questions