Questions tagged [subdocument]

Concept in document-oriented databases, where a field in document is a document in itself.

Concept in document-oriented databases, where a field in document is a document in itself.

Example document as JSON, where address is a subdocument:

{
    name: "Snake",
    address: {
       street: "742 Evergreen Terrace",
       city: "Springfield"
    }
}
288 questions
324
votes
7 answers

Stop Mongoose from creating _id property for sub-document array items

If you have subdocument arrays, Mongoose automatically creates ids for each one. Example: { _id: "mainId" subDocArray: [ { _id: "unwantedId", field: "value" }, { _id: "unwantedId", field:…
Atlas
  • 3,343
  • 2
  • 13
  • 9
36
votes
3 answers

Mongoose Subdocuments in Nest.js

I'm moving my app from express.js to Nest.js, and I can't find a way to reference one mongoose Schema in another, without using old way of declaring Schema with mongoose.Schema({...}). Let's use example from docs, so I can clarify my…
LBeerus
  • 391
  • 1
  • 3
  • 7
13
votes
1 answer

MongoDB remove an item from an array inside an array of objects

I have a document that looks like this: { "_id" : ObjectId("56fea43a571332cc97e06d9c"), "sections" : [ { "_id" : ObjectId("56fea43a571332cc97e06d9e"), "registered" : [ "123", …
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
13
votes
2 answers

mongoose - ObjectId that references a Sub-Document

Would it be possible for an ObjectId in ModelA to reference a sub-document in modelB? var C = new Schema({...}); var B = new Schema({c: [C]}); var A = new Schema({c: { type: ObjectId, ref: 'ModelB.ModelC' }); var Model_A =…
Shakirullahi Logico
  • 249
  • 1
  • 2
  • 10
7
votes
2 answers

MongoDB 3.4 - get array of subdocuments without root document

I have a projects collection with documents such as this: { "_id" : ObjectId("589eff3fee3d13019843f55a"), "name" : "Project A", "desc" : "test", "numofvms" : 0, "templates" : [ { …
NoBodyIsPerfect
  • 173
  • 2
  • 9
7
votes
3 answers

Mongoose for var in loop in subdocuments

I created this schema with mongoose Schema : socialAccount = new Schema({ socialNetwork : { type : String , required : true}, userid : { type : Number, required : true }, username : String },{_id : false}); person = new Schema({ …
M2sh
  • 741
  • 1
  • 11
  • 23
6
votes
1 answer

How to retrieve a specific field from a subdocument array with mongoose

I'm trying to get a specific field from a subdocument array I'm not gonna include any of the fields in the parent doc Here is the sample document { "_id" : ObjectId("5409dd36b71997726532012d"), "hierarchies" : [ { "rank"…
Austin Davis
  • 3,516
  • 10
  • 27
  • 47
6
votes
1 answer

insert in subdocument with mongoDB

I have the following document in the collection: "_id" : "2", "workspace" : [{ "name" : "1", "widgets" : [ ] },{ "name" : "2", "widgets" : [ ] },{ "name" : "3", "widgets" : [ ] },{ …
Ricklemer
  • 201
  • 1
  • 2
  • 10
5
votes
1 answer

Mongoose populate() returns empty array

I want to query the subdocument array by the property 'token' in clientSchema. But I'm not able to populate the subdocument array. It always returns empty value. This is what I'm tried var performAuthAsync = promise.promisify(performAuth); var…
MohanRajNK
  • 895
  • 2
  • 13
  • 26
5
votes
1 answer

editing subdocments N-N relationship in mongodb

I have an application where an article can be linked to multiple platforms. Article contains a list of platforms and platforms also contains a list of articles. For more detailed information please look at this stackoverflow question that I asked a…
WebstraDev
  • 521
  • 3
  • 13
5
votes
1 answer

Update a MongoDB subdocument when the parent document might not exist

Here's my data, consisting of a books collection, with a books.reviews sub-collection. books = [{ _id: ObjectId("5558f40ad498c653748cf045"), title: "Widget XYZ", isbn: 1234567890, reviews: [ { _id:…
Horace
  • 346
  • 2
  • 14
5
votes
1 answer

Mongoose doesn't save nested sub documents

I need to save some sub documents in a schema which is sub document of a schema. The save function is this: exports.add = function(req, res){ var cliente = new Cliente(req.body); var sedi = []; for(var key in req.body.sede){ var sede = new…
pindol
  • 2,110
  • 6
  • 35
  • 52
5
votes
1 answer

In Mongodb subdocument array is there any way to add a new field in each subcoument

Suppose i have a document like { "_id" : 5, "rows": [ { "id" : "aab", "value":100}, { "id" : "aac", "value":400}, { "id" : "abc", "value":200}, { "id" : "xyz", "value":300} ] } and i need to add a new key…
P K P
  • 77
  • 5
4
votes
1 answer

MongoDB/Mongoose Updating Whole Sub/Nested Document

I have 2 schema User Schema const usersSchema = new schema({ name: { type: String, required: [true, "name is required"], unique: true }, email: { type: String, required: [true, "email is…
Jsennin
  • 153
  • 2
  • 12
4
votes
2 answers

MongoDB/Mongoose - Adding an object to an array of objects only if a certain field is unique

So I have a nested array of objects in my MongoDB document and I would like to add a new object to the array only if a certain field (in this case, eventId) is unique. My question is very similar to this post, only I cannot seem to get that solution…
Gabor Szekely
  • 1,108
  • 5
  • 14
1
2 3
19 20