Reputation: 23
I am using Mongoose and NodeJS. I want to create a document, in some collection only if document in another collection exists, without making two queries to the database.
For an example lets say we have a collections of locations and apartments. Location _id is passed on the creation of a new apartment and the apartment stores it. So I want to create a document for new apartment only if location with such an _id exists. I know that I can check for a location, using query, and then to create apartment only if the query return a result, but by this way I'am making two queries to the DB and the code becomes hard to mange.
I'am asking if there is a better way to this by telling MongoDB to do this check for me.
Upvotes: 0
Views: 1094
Reputation: 38519
You'll need to make two queries.
There's no such thing as referential integrity in mongodb
Upvotes: 0