MongoDB _id field - node.js

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?

Related

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

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

How to retrieve schema from a mongoose collection?

using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only

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 access multiple collection

In mongodb I have one collection named as People.
It contains:
_id:Number,
name:String,
friends:[{friendsId:{type:Schema.Types.ObjectId,ref:'People'},addedTime: Date}]
I want to search a name of friend of a particular user and return the _id of that friend in a single query using mongoose in node.js.
Its impossible to retrieve the data in a single query/populate. Since mongodb is not a relational database. Iterate the data separately & give the result as callback to next query.

Resources