I am trying to read from a Mongo database using mongoose where the models make use of the discriminator inheritance functionality, but the documents in the DB are all inserted by another service (using the Java Mongo driver) which does not use mongoose nor its discriminators. All of my queries using subclass models (those which use the discriminator function) return empty arrays when I try to read from the DB. I think it's because mongoose is expecting those documents to contain a discriminator key, however the service which is inserting the documents has no knowledge of discriminator keys, and thus isn't setting them on the mongoDB documents.
How can I create my models and use the discriminator function such that they can still query for these documents inserted by another service?
For more context, I want to use discriminators because inheritance allows me to cleanly structure the fields of the models I'm creating and define model-specific static methods, and it lets me not write duplicate code. If there is a better way to accomplish these goals without using mongoose's built-in discriminator pattern, please share!
According to the documentation:
The way mongoose tells the difference between the different
discriminator models is by the 'discriminator key', which is __t by
default. Mongoose adds a String path called __t to your schemas that
it uses to track which discriminator this document is an instance of.
Also mongoose saves documents with discriminators to the single collections.
So, in order to have access to the documents you need to save __t parameter, and check if you save schemas with the same discriminators to a single collection
Related
I would like to create collection where only one entity should be stored.
Questions:
Is it possible to create restrictions for this collection on db level?
What are the best practices for CRUD application dealing with such collection?
You can use schema validation to be sure that new documents will respect your schema. This feature appears in 3.2, but was modified since 3.6 with use of JSON schema. Check the right doc version.
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.
You'll need to make two queries.
There's no such thing as referential integrity in mongodb
I want to clone an existing collection, including data and indexes, to a new collection with another name within the same database, using mongodb JSON interface (not the command-line interface).
I've tried:
cloneCollection - didn't work. is for cloning across databases.
aggregate with an $out operator - that copies just the data but not the indexes.
The aggregate command I've tried:
{"aggregate":"orig_coll", "pipeline":[{"$out":"orig_clone"}]}
There is no way to do this in one JSON query.
So, two solutions here :
Using mongodump/mongorestore as proposed in What's the fastest way to copy a collection within the same database?
Using two queries : one to create the destination table with the index and the aggregate query that you already have. I understand that it's not a perfect solution as you need to maintain the query to create the index on the destination table and the index on the source table but there's no other way to do this.
What you need to understand is that, the JSON interface as you told it is not a database interface but a database JavaScript query language. So you can pass query to it not command. In fact, it's not an interface just a query DSL. The interface is the mongo shell or any of the mongo drivers (java, perl, ...) or any of the mongo admin tools ...
I'm currently learning MongoDB why do we use .populate(). What is the difference between .populate() and .virtual()?
Can anyone tell me how they differ and when one should be used over the other?
Both serves a different purpose and one cannot be a replacement of other.
Virtual: If you want attributes that you can get and set but that are not themselves persisted to mongodb, virtual attributes is the Mongoose feature for you.Remember, only non-virtual properties work as part of queries and for field selection.See Virtual
Populate: ObjectIds can refer to another document in a collection within our database and be populate()d when querying.It gives you DBRef-like behavior. See Populate
What I've understood, sails binds collection with its Models. Is there a way i can create a collection on runtime. What i want to do is create a different collection for each user. something like (user_12345, unique for every user).
I've tried waterline and sails-mongo, They allow me to query collections for CRUD operation. but I couldn't understand how to create a new collection using sails-mongo or waterline library. Please help.
Waterline doesn't provide a way to create arbitrary collections in Mongo. The ORM's job is to map the logical models in your app to collections (or tables, or whatever) in your database. It's not clear what you're trying to accomplish by giving each user their own collection, but if it's truly necessary you can still achieve it by just making the raw Mongodb driver a dependency in your project:
npm install --save mongodb
and following the documentation for connecting and creating collections. You won't be able to use Waterline to work with those collections, though; you'll have to use native operations for all inserting, querying, etc.