Last Updated
Viewed 09 Times

I have a Categories Collection laying on My Mongo Atlas Server. Getting my Category Object isn't a problem. My object looks like that.

{
    "_id": "5de246a80a5dd651fca64dbf",
    "category_id": 1,
    "category_name": "Die erste Kategorie",
    "sub_categories": [
        {
            "_id": "5de246a80a5dd651fca64dc0",
            "subcategory_id": 11,
            "subcategory_name": "Die erste Unterkategorie"
        }
    ]
}

Now I want to patch my sub_categories Object with my API. Right now I just want to add new Elements of sub_categories. Later I also want to delete elements. Already tried out .length() , count() etc. didnt work for me

router.patch('/:id', getCategory, async (req, res) => {
    if (req.body.category_name != null) {

        res.category.category_name = req.body.category_name
    }
    if (req.body.category_id != null) {
        res.category.category_id = req.body.category_id
    }
    if (req.body.subcategory_id != null) {
        // var x= res.category.sub_categories.length()
        //var x= { $size: <res.body.sub_categories> }
        //res.body.sub_categories.count()
        //res.category.sub_categories[x].subcategory_id = req.body.subcategory_id
    }
    try {
        const updatedQuestion = await res.question.save()
        res.json(updatedQuestion)
    }
    catch (err) {
        res.status(400).json({ message: err.message })
    }
})

Any Ideas?

I have an array as a subdocument in the Mongoose model defined as follows:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var schema = Schema({
    author: { type: Schema.Types.ObjectId, ref: 'Author' },
    pages: [{
        number: Number, 
        type: { type: String, enum: ['boring', 'fun']},
    }]
});

module.exports = mongoose.model('Book', schema);

Now I want to get the total number of 'fun' pages across all books by a certain author. Many similar questions (and answers) I found discuss how to find Books with fun pages in this scenario, however I want to get an array of all fun pages, or at least the length of that array, but if possible I would like to get the array itself. How can I do this?

Thanks,

I have a model which looks like this:

User.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var memberSchema = new Schema({
        email: { 
            type: String, 
            required: true, 
            unique: true 
        },
        password: { 
            type: String, 
            required: true,
            min: 8
        }
});

var userSchemaPrimary = new Schema({
    team_code : {
        type: String, 
        required: true, 
        unique: true 
    },
    members:[memberSchema],
});
var User = mongoose.model('User', userSchemaPrimary);

module.exports = User;

And this is how am trying to save

var User = require('../models/user');
var newTeam = new User({
    team_code : 'CODE01',
    members:
        {
            email: req.body.email,
            password: pass
        }
});

newTeam.save(function(err) {
    if (err) throw err;
    console.log('User saved successfully!');
    return res.send("Done");
});

When executed, throws model validation error. Well I tried to save data without the array documents, then its saves successfully. But when I try to save the array (array "members"), it throws validation error.

I WANT TO

Store data in the following way:

{
   team_code: "CODE01",
   members: [
      {
         email: "test01@email.com",
         password: "11111111"
      },
      {
         email: "test02@email.com",
         password: "22222222"
      }
      {
         email: "test03@email.com",
         password: "33333333"
      }
   ]
}

I dont understand what is going wrong. Any help is appreciated.

I've got such schema:

username: String,
authData: socialNetworkName: {
        access_token: String,
        expiration_date: Date,
        id: String
    }

And example of using it:

"name" : "Ilya",
"authData" : {
    "fb" : {
        "access_token" : "ECBAI3easd64T4dEokBaANN",
        "expiration_date" : ISODate("2017-02-01T18:55:07Z"),
        "id" : "4314342324242424242"
    },
    "twitter": {
        "access_token":"fdfkdlfdfdfkdl",
        "expiration_date":ISODate("2017-02-01T18:55:07Z",
        "id":"343243434"
    }
},

How do I access fb in authData in order to update access token without changing 'twitter'?

Similar Question 9 (2 solutions) : updating element inside nested array mongoose

cc