Note that there are some explanatory texts on larger screens.

plurals
  1. PO_id in sub-objects in a mongodb document (using Mongoose)
    text
    copied!<p>I am creating a structure like this in Mongoose:</p> <pre><code>var Access = new Schema({ userId : { type: ObjectId, unique: true }, key : { type: String, index: true }, isOwner : { type: Boolean, index: true }, }); mongoose.model('Access', Access); var Workspace = new Schema({ name : { type: String, lowercase: true, unique: true}, activeFlag : Boolean, settings : { welcomeMessage : String, invoiceTemplate : String, longName : String, defaultCountry : String, countryId : { type: ObjectId, index: true }, }, access : [ Access ], }); mongoose.model('Workspace', Workspace); </code></pre> <p>After adding some documents, I see the result:</p> <pre><code>{ "activeFlag" : true, "name" : "w7", "_id" : ObjectId("5036131f22aa014c32000006"), "access" : [ { "user": "merc", "key" : "673642387462834", "isOwner" : true, "_id" : ObjectId("5036131f22aa014c32000007") } ], "__v" : 0 } </code></pre> <p>I am confused by that <code>_id</code> in the sub-document, which doesn't seem to happen if I add just it as a sub-structure rather than a sub-schema. So questions:</p> <p>1) Where does that <code>_id</code> come from? Is Mongoose's driver doing it? If so, how can I reach the same behaviour using straight Mongodb? Just by adding an ObjectId field?</p> <p>2) When would you use a sub-document, and when would you use just a data structure?</p> <p>3) I haven't gotten started with the serious part of my web application yet. However, wouldn't that ID there be <em>really</em>, and I mean <em>really</em> useful in case you are allowing JsonRest access to a sub-record in a document for example?</p> <p>Thank you as ever!</p> <p>Merc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload