mongo-join-query
Allows you to query your database using linked collections in your query.
Imagine in your database you have Teams linked by ID to Players, which are linked by ID to Schools.
What if you want all teams with players that went to a school founded after 1950? Or if you wanted a
team object with all members embedded? What if their school were also embedded to them?
mongo-join-query
allows you to do exactly that. You can populate as many levels deep as you want.
Note: You need to be using Mongoose to specify your database schema.
Note: Requires Mongo >= v3.4.
mongoJoin(
mongoose.models.Team,
{
find: { "members.studiedAt.yearFounded": { $gt: 1950 } },
populate: ["members.studiedAt", "leader.studiedAt"],
sort: { "members.age": 1 },
skip: 10,
limit: 1
},
(err, res) => (err ? console.log("Error:", err) : console.log("Success", res))
);
How does it work?
Behind the scenes the options object is transformed in different stages in an
aggregation pipeline.
To see the entire aggregation pipeline being used add debug: true
to the options object.
Limits and Contributing
You can populate an array field if it is a property of the main Model of the query. But you cannot
populate an array field of linked models.
This is a shortcoming of the current implementation of the $group
phase of the aggregation. If you
would like to contribute with this project, this is the place to look at.
When contributing please make sure to include a failing test int he pull request.
Docs
The library exposes a single function which accepts three arguments:
mongoJoin(
mongoose.models.Team,
options,
callback
);
Options
const options =
{
find: { "members.studiedAt.yearFounded": { $gt: 1950 } },
populate: ["members.studiedAt", "leader.studiedAt"],
sort: { "members.age": 1 },
skip: 10,
limit: 1,
debug: false
},
Callback
The callback takes two arguments:
- error - An error object. If there is no error it will be
null
. If there is an error, it will
looks like this:
{ name: 'Invalid query',
message: 'Invalid population path: Field nonExistent not found.',
errors: []
}
- result - If there is an error, the result will be null. If there isn't it will look like this:
{ name: 'Invalid query',
message: 'Invalid population path: Field nonExistent not found.',
errors: []
}
Full Example
Here is an example:
const m = require("mongoose");
const mongoJoin = require("mongo-join-query");
const School = m.model(
"School",
m.Schema({
name: String,
yearFounded: Number
})
);
const Player = m.model(
"Player",
m.Schema({
name: String,
age: Number,
studiedAt: {
type: m.Schema.Types.ObjectId,
ref: "School"
}
})
);
const Team = m.model(
"Team",
m.Schema({
name: String,
leader: {
type: m.Schema.Types.ObjectId,
ref: "Player"
},
members: [
{
type: m.Schema.Types.ObjectId,
ref: "Player"
}
]
})
);
m.connect("mongodb://localhost/test");
m.connection.once("open", () => {
mongoJoin(
m.models.Team,
{
find: { "members.studiedAt.yearFounded": { $gt: 1950 } },
populate: ["members.studiedAt", "leader.studiedAt"],
sort: { "members.age": 1 },
skip: 10,
limit: 1
},
callback
);
});
function callback(error, res) {
if (error) {
console.log("An error occurred:", error);
return;
}
console.log("Total of results found:", res.count);
console.log("Values returned: ", JSON.stringify(res.results, null, 4));
}