I'm trying to get a list of books with their author information.
Some users were deleted and therefore they have not longer a document in the db, therefore, their info is null.
I'm trying to pull books ONLY if their creators still exist.
This is my code:
Book.find({_creator:{$ne:null}}).populate(
{
path: '_creator',
match: { _id: { $ne: null }}
})
.exec(function (err,books) {
if(err) throw err;
if(books) {
res.send(books)
}
})
This is what it returns:
[
{
"_id":"55d98e6a4de71010099c59eb",
"dateOfCreation":"2015-08-23T09:12:10.095Z",
"_creator":null,
"__v":0,
"coverUrl":"cover14403211323926quv.png",
"description":"asdasd",
"name":"asdasd"
}
]
Notice that the _creator field IS null. Why is that?
you need to understand the order of execution of your code:
mongoose gets all books from the database where {_creator:{$ne:null}}
. Mongo is only looking at the reference inside the books collection to determine which documents to return. Your book still has a reference to an author, and mongo will not notice that there is no matching Author in the Authors collection, so your book is loaded.
mongoose is populating all returned results: so it is loading the authors from the Authors collection and replaces the references with the real objects. For your book it does not find a matching author, so it puts the null
there.
Thats why you end up with your resultlist.
Mongo does not support joins - therefore you cannot do a query that includes data from more than one collection. Populate is just a way to replace references in your resultlist with real data, you can never use populated data as part of your where clauses.
To solve your issue you can either:
AuthorSchema.post('remove', function(doc) {// update your books here});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With