Global dictionary filed for mongoengine documents - mongodb

I want to dynamically update a global dictionary attribute for a collection in mongoengine. My task is to read a number of documents and annotate them with different descriptions. I want to update a global dictionary whenever a new description is added so that it is available for subsequent documents. How is this possible?
I hope that makes sense.

Since MongoDB is schema-less, you could store the global object in a the collection and it update it that way
class NormalDoc(mongoengine.Document)
attr1 = mongoengine.StringField()
# global attribute hidden in the collection
global_dict = mongoengine.DictField()
is_global = mongoengine.BooleanField(default=False)
There are better ways todo this (like putting it in a separate collection), but that would work if I understand correctly

Related

Is there any possible do upsert functionality in "array of an object" using firestore query?

Example :[{
inst:"EVA",
std:"12th"
},
{
inst:"KSF",
std:"12th"
}]
As per the above example, In my case if "inst: "EVA" is already there in the "qualification" array so we need to update the object from the existing one.
Then "inst: "KSF" does not already exist in the "qualification" array so we need to add that one.
Help me if there is any way to upsert using firestore query.
There is no "upsert" operation for objects in arrays. If you need to make changes to that array, you will have to read the document, modify the contents of the array in memory, then update the document with the new contents of the array.
Arrays of objects usually do not work the way that people want, given their limitations on querying and updating. It's usually better to store data as documents in a nested subcollection, so they can be more easily queried and updated by the contents of their fields.

mongodb schema design, should I create separate collection to store tag

I am designing schema for mongodb and I need advice on one design issue. One of my collection (A) has a field (F_1) which can have value from a list of predefined values (item may insert/delete later in this list). My issue is should I use just text field to store F_1 or should I use collection to store F_1 with repopulated list and use reference on A. Please advice or point me to documentation.

MongoDB Bulk Find and Replace of ObjectId on a single Document

We have two documents that have merged and they now have one one ObjectId.
There exists a configuration document that may have references to the old ObjectId. The old ObjectID can exist all over this document which is full of nested arrays and lists.
We want to do a simple find and replace on this document, preferably without replacing the entire document itself.
Is there a generic way to set every field that has ObjectIdA as a value and replace it with ObjectIdB?
There's no way to do that, no. You need to perform updates on all possible paths explicitly.

Is it possible to delete a field with MongoEngine, without strict=False?

I've got a lot of data in MongoDB, which we access primarily via MongoEngine, and sometimes data first ended up in field F1, and then we later decided that field F2 is a better place for it, so we moved it over there, and stopped using F1.
That's convenient, but now we've got a bunch of stale (or useless) data in old F1 keys, and new documents are being created with empty F1 keys, for no reason.
While MongoDB being schemaless is convenient, I still appreciate the strict=True feature (which is on by default), and try to avoid turning it off except when absolutely necessary. I don't like turning off all the safety checks on a collection.
So is there any way to delete a field F1 from my MongoDB collection, without downtime, and without strict=False?
If I remove the field from my Document subclass first, MongoEngine will complain when it tries to load existing documents.
If I remove the field from my database first, MongoEngine will create it for any new records, until the model is updated.
Is there any way with MongoEngine to say "This is an old field. You can load it (or ignore it) if it's there, but don't create it for any new documents"?
If I remove the field from my database first, MongoEngine will create it for any new records, until the model is updated
It's only true if you explicitly write to that field or if the field has a default value set. Otherwise the field won't exist in MongoDB.
So as first step I suggest to remove the code that writes to that field and remove the default value (or set it to None). Then it's safe to remove the field from the database.
Below a small proof:
import mongoengine
class Foo(mongoengine.Document):
a = mongoengine.IntField()
b = mongoengine.ListField(default=None)
f = Foo().save()
type(f.a) # NoneType
type(f.b) # NoneType
And the database query:
> db.foo.findOne()
{ "_id" : ObjectId("56c49ae8ee8b341b4ea02fcb") }

How to move object from one collection to another without changing _id

I have a queue mechanism which I want to keep small, so all queued objects, after they are done processing are moved to a different collection called history, where they are not updated anymore and are only there for reference and info.
Is it possible to take an object from one collection, remove it and insert it into another collection without changing the _id ?
I now solved this by creating a second id in the schema which I transfer to the new object, but I'd rather keep referencing _id.
Also if you think I'm overlooking something and don't need a second collection to keep my queueing mechanism fast, I'd love to hear about it.
Here's how I currently do it (using step and underscore)
`
// moving data to a new process in the history collection
var data = _.omit(process.toObject(), '_id');
console.log("new history data", data);
var history = new db.History(data);
history.save(this.parallel());
db.Queue.remove({_id : process._id }, this.parallel());
`
You can copy/move a doc from one collection to another without changing its _id just fine. If you create a new doc that already has an _id then Mongoose/Mongo will use it. And _id values only need to be unique within a single collection, not between collections.
I tried using delete delete object._id; in order to remove the property and allow mongodb to assign one itself, but for some reason delete did not work.
This works for me:
obj['_id'] = undefined;
If you are not going to use the _id then this will fix your problem.