Mongoose: exclude _id field in inserts - node.js

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).

Related

How to add timestamps to existing records in MongoDB

I am having a collection 'users' in MongoDB which contains multiple records without timestamps. I am using that collection with my node application and have set timestamps to true as shown:
const userSchema = new Schema({
...
},{
timestamps: true
});
I wanted to apply timestamps to the existing records and use it with my node application in future. If I make new fields 'createdAt' and 'updatedAt', will they work with my Mongoose schema? Or if there is any alternative way to achieve the task, please enlighten me as I am new to node and mongo in general.
first of all, I think this applies cannot affect the existing collections in the database, cause these fields are just a bunch of documents you inserting with existing/updating operations.
in MongoDB, everything is just a document and MongoDB does not care about data you store inside a collection, no validation here.so mongoose comes in for handling those validations and etc. if you change a schema in a collection it only effects to incoming requests from now on. but be careful if conflict fields happen, you will get an error for getting collections.
in short answer: MongoDB does not know when data stored or edited
but you can get timeStamp of creation in mongo ObjectId:
https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/index.html

mongoose schema model _id

I'm using mongoose 5.0.9. My question is about the _id of document.
I'd like to get `_id generated from mongodb when new document inserted. In this way, I could not define the _id in my mongoose schema/model. If I do, the mongoose will fail since I don't provide the value for _id.
However, I want to use the _id in my page. If I don't define it in my schema/model, I could not get it when querying.
One solution I thought is to define two schemas: one for inserting document without _id, and the other is for querying, updating, etc, with _id.
I don't want to make things to complex, so I don't think this is good idea.
what is the best practice for this _id?
Thanks in advance.
Richard

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.

Mongoose - create model from existing db data

Using Mongoose, I have a document that was previously pulled from the database, complete with an _id property, in raw Object format (IE, without all of the document methods, just straight from the db).
How can I use that data to create an instance of mongoose.Model without the system assigning the model a new _id? I want to then eventually save that model and have it update the existing document in the database.
Update: using a combination of #Jack Newcombe's method, and subsequently setting model.isNew to false, I get the following error: "Mod on _id not allowed". So now it knows to update, but Mongoose is not removing the _id field from the update request. There has to be one more system property on the Model that tells Mongoose whether or not to remove the _id during an update request. Any ideas?
I've figured it out, but I'm sure there is a better way. I'm surprised there isn't a way to do this easily with Mongoose's API.
Anyway, you need to the following:
Create the model like this: var model = new Model(data,
{_id:false});
Manually set model.isNew to false
Manually tell Mongoose that the _id field hasn't been modified, like this: delete model.$__.activePaths.states.modify._id
The reason I'm not so fond of this is because I'm taking advantage of the fact that JavaScript doesn't have true protected methods, and I'm basically hacking Mongoose in order to get it to work. So I'd love to hear other answers if anyone has.
You can prevent the schema for the model from automatically generating an _id by passing in an option that sets the _id to false:
var schema = new Schema({ name: String }, { _id: false });
Source: http://mongoosejs.com/docs/guide.html#_id

Resources