简体   繁体   中英

How to add to this collection in MongoDB

So I have a EmployeeSchema that takes in a user and an array of employees. Each employee will have an email, name, jobtitle, and a questionandanswer array. I want to be able to add to the questionandanswer array, but I don't know how to find the collection using the user.id and emplyoee.email or employee.id. Let's say for example I wanted to find matt@concur.com and add to the questionandanswer array.

{
"_id": {
    "$oid": "5b7bb55197243619089d5a2f"
},
"user": {
    "$oid": "5b7aeb3cf0861f453c279910"
},
"employee": [
    {
        "_id": {
            "$oid": "5b7bb8893b55b9192cd84f58"
        },
        "email": "matt@concur.com",
        "name": "Matt J",
        "jobtitle": "HR Manager",
        "questionandanswer": []
    },
    {
        "questionandanswer": [],
        "_id": {
            "$oid": "5b7bb5db3b55b9192cd84f57"
        },
        "email": "john@concur.com",
        "name": "John Doe",
        "jobtitle": "Software Engineer"
    }
],
"__v": 2
}

this is what my add employee looks like right now:

const employeeFields = {};
employeeFields.user = req.user.id;
if (req.body.email) employeeFields.email = req.body.email;
if (req.body.name) employeeFields.name = req.body.name;
if (req.body.jobtitle) employeeFields.jobtitle = req.body.jobtitle;

Employee
    .findOne({
        user: req.user.id
    })
    .then(employee => {
        employee.employee.unshift(employeeFields);
        employee.save().then(employee => res.json(employee));
        //new Employee(employeeFields).save().then(employee => res.json(employee));
    })

You can use the following syntax of the query to find the employee from the array of employee

db.Employee.findOne({ user.$oid : req.user.id, "employee" : { $elemMatch :{ email: req.body.email } } }, function(err, employee) {
   employee.questionandanswer.push(" your question and answer object from request ");
   employee.save();
});

Try this and let me know if it works.

based on what you have mentioned in your question, correct me if I mis understood. you're trying to filter data based on what you have in request's body:

It is better, from performance perspective, when you find and update in just one simple query, that is why I used here findOneAndUpdate

Additionally, I would like to highlight that I noticed that you have employee defined as an array that is why I used employee.0 or employee.1 so if you have an employee property as an object you can simply ignore 0 and 1 .

let employeeFields = {};
employeeFields["user.$oid"] = req.user.id;
if (req.body.email) employeeFields["employee.0.email"] = req.body.email;
if (req.body.name) employeeFields["employee.0.name"] = req.body.name;
if (req.body.jobtitle) employeeFields["employee.0.jobtitle"]  = req.body.jobtitle;

db.Employee.findOneAndUpdate( employeeFields , "$push": { "employee.1.questionandanswer": **your question and answer object**});

Have a look and let me know whether it works, if not plz leave a comment to understand your issue and be able to better assist you.

More help: https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM