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.
Related
Iam using nosql in totajs4 .
Document structure in nosql database
{directoryName:{fileName:{"created": timestamp,"modified":timestamp,"deleted":timestamp}}}
unable to update this structure.
Changing nested objects isn't supported yet in Total.js 4. I recommend reading the specific document and modify only the nested property/key.
Since MongoDB 3.2 you can set intra-collection constrains, as described in this post.
However, what avour inter-collection constraints (such a the typical foreign key constrain in relational databases to ensure a linked document exists)? Has been something like that being included in recents versions of MongoDB? Is there any plan to do so?
Thanks!
I am unaware of any functionality like that in MongoDB server.
There is schema validation but it also applies only within one collection at a time.
If you use an ODM, some of this may be provided by the ODM. For example, Mongoid offers dependency constraints.
I have an existing MongoDB without the _cls field in the documents.
Data will continue to enter the DB during the lifetime of the DB, data is added through Morphia which doesn't add the _cls field automatically.
It seems not the best idea to add a _cls field to Morphia Entities.
Do you have a better idea how to make the data coming from morphia better fit (have the _cls field) in mongo documents?
Edit:
*I am using flask server in python with mongoengine which require the field
I saw the solution for using #PreSave in Morphia, it is a good Idea and I will use it if another solution is not found. ** I am looking for a solution in the Python side**. you aren't always able to change the data insertion
You could use Morphia’s #PreSave annotation to ‘tweak’ the JSON document prior to it being saved to the database. You could then just inject the _cls field and value without having to declare a field in your Java class
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
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.