Adding a field to all documents MongoDB - node.js

So I want to add a field called MiddleName to all documents in my collection
db.users.updateMany({}, {$set: {MiddleName: ""}});
but for some reason the code above doesn't do anything to my documents

You need to use the multi:true flag. It will update the existing documents in that collection.
db.users.update({}, {$set : {"MiddleName": ""}}, {multi:true});

Related

How to figure out if _id field in MongDB was generate by mongo or passed by user during insert

I have an app where we pass _id to mongodb during insert instead of using the build in _id generation logic. Now in my DB I see lots of document with _id:Object('')
I need to find count of all the documents who's Id were generated by MONGO, anyone knows how??
You can use the MongoDB $type query operator.
db.collection.count({ _id: { $type: "objectId" } })

mongoose update fields and add new field

I already googled a lot but unfortunately, I'm unable to solve my problem...
I've some documents in my MongoDB and now I added a new optional field to my mongoose schema. Unfortunately, I cannot update my existing documents with this new field.
MyModel.findByIdAndUpdate(req.body.modelId, {$set: req.body}, {new: true})
This only updates the existing fields but unfortunately it doesn't add the new field.
How can I force mongoose to add the new field to the document?
Update: My mistake. My schemas are having their own repository because I'm sharing them between different projects. I forgot to update them in the respective project. Now everything works perfect.
You should update the document like this:
Model.update({_id :req.body.modelId },{$set : {new_field: "value"}})
Mongoose is probably doing something between, so it is better to bypass it.

Mongoose: exclude _id field in inserts

I have a high-concurrency and parallelism situation and would like _id fields to be created by MongoDB and not by mongoose, so that I can use the ObjectId timestamps in the _id field to reliably query documents in the order they were inserted.
Is this possible? Right now I don't see how to do this with mongoose, as marking a schema with {_id: false} and trying to save it returns an error document must have an _id before saving.
Mongoose docs say the _id option only works on subdocuments, hence the error you get (http://mongoosejs.com/docs/guide.html#_id).
It might fit your situation to add a document without mongoose and in a subsequent operation update it through using mongoose (thereby keeping mongoose's Schema functionality etc).

MongoDB _id field

I'm having trouble understanding with MongoDB is doing with my documents. I have a collection of a certain document. This document has a schema (I'm using Node.js Mongoose server-side). It has a couple of arrays of sub-documents. When I save a document that does not have an _id field Mongo should generate an _id field which it does. However, some of my 'sub-documents' also seem to have been given an _id field and other types of custom sub-documents were not. What the heck is going on here? Shouldn't I just get one _id per document?

Stop Monk from adding _id to docs

Is there a way to stop monk from generating and adding _id to docs ?
Or is there a driver for MongoDb in Node.js that doesn't auto add _id ?
_id is needed in every doc. If you don't want auto generated _id then you have to add a custom one. But every doc should have _id in mongo.
You can add your own _id and insert it when inserting doc. Just make sure _id is unique across all docs in a collection. It should be what primary key is in RDBMS.
db.createCollection("noautoid", { autoIndexId: false });
This will create a collection without _id but the option is deprecated and will be removed in next versions.

Resources