Schema:
var educationSchema = new Schema({
schoolName: String,
startDate: Number,
endDate: Number,
degree: String,
major: String,
grade: String
});
var UserSchema = new Schema({
firstName: String,
lastName: String,
education: [educationSchema]
});
Update code:
User.findOneAndUpdate(
{"_id": req.user.id, "education._id": req.body.id},
{
"$set": {
"education.$": req.body
}
},
function(err, edu) {
}
);
Now, if the user only edits the schoolName on the UI the following happens:
Pre-save State:
{
"_id" : ObjectId("5878fb4f51ec530358fea907"),
"firstName" : "John",
"lastName" : "Doe",
"education" : [
{
"schoolName" : "ABC",
"startDate" : 1998,
"endDate" : 2005,
"degree" : "Bachelor’s Degree",
"major" : "CS",
"grade" : "3.5",
"_id" : ObjectId("5878fbb951ec530358fea909")
}
]
}
Post-save State:
"education" : [
{
"schoolName" : "XYZ"
}
]
Is $set not the right operator to use?
Updating education.$ updates the sub-document. If you want to update only the schoolName you must use education.$.schoolName.
Change your update code to:
User.findOneAndUpdate(
{"_id": req.user.id, "education._id": req.body.id},
{
"$set": {
"education.$.schoolName": req.body
}
},
function(err, edu) {
}
);
EDIT: (update any field sent through req.body)
const update = {};
Object.getOwnPropertyNames(req.body).forEach(key => {
update['education.$.' + key] = req.body[key];
});
User.findOneAndUpdate(
{"_id": req.user.id, "education._id": req.body.id},
{
"$set": update
},
function(err, edu) {
}
);
Related
I'm at the learning stage yet.
I am having trouble establishing a relationship in Mongodb.
error not found 404
I made all the operations in the collections with the data in both tables, and in the training I received. But somehow I couldn't run it successfully.
Models Books
const Book = require('../Models/Book');
router.post('/new', function(req, res, next) {
const book = new Book({
title: 'Hikayelerde ve çocuk',
userId: '5ed267005e5d568b58cd17f7',
published: true,
category: 'Hikaye',
comment: [
{
author: "Macit",
mail: "macit#macit.com",
subject:"Kitap fena değil",
message: "Bu kitap oldukça güzel ben çok beğendim ",
},
{
author: "Mutlu",
mail: "mutlu#mutlu.com",
subject:"Kitap fena değil",
message: "Bu kitap oldukça güzel ben çok beğendim ",
},
],
meta:
{
votes: 3,
favs: 3
},
});
book.save((err, data) => {
if (err)
console.log(err);
res.json(data);
});
});
// aggregate $lookup
router.get('aggregate-lookup', (req, res) => {
Book.aggregate([
{
$lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'user'
}
}
], (err, result) => {
res.json(result);
});
});
module.exports = router;
Books Collection
/* 1 */
{
"_id" : ObjectId("5ed26a1ca997a08c2d6d59bf"),
"published" : true,
"title" : "Hikayelerde ve çocuk",
"userId" : ObjectId("5ed267005e5d568b58cd17f7"),
"category" : "Hikaye",
"comment" : [
{
"_id" : ObjectId("5ed26a1ca997a08c2d6d59c0"),
"author" : "Macit",
"mail" : "macit#macit.com",
"message" : "Bu kitap oldukça güzel ben çok beğendim "
},
{
"_id" : ObjectId("5ed26a1ca997a08c2d6d59c1"),
"author" : "Mutlu",
"mail" : "mutlu#mutlu.com",
"message" : "Bu kitap oldukça güzel ben çok beğendim "
}
],
"meta" : {
"votes" : 3,
"favs" : 3
},
"publishedAt" : ISODate("2020-05-30T14:13:48.440Z"),
"__v" : 0
}
Book
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BookSchema = new Schema({
title: String,
comment: [
{
author: String,
mail: String,
subjet: String,
message: String,
},
],
category: String,
meta: {
votes: Number,
favs: Number
},
published: {
type: Boolean,
default: true
},
publishedAt : {
type: Date,
// şuanki tarih
default: Date.now
},
userId: {
type: mongoose.Schema.Types.ObjectID
},
});
module.exports = mongoose.model('Book', BookSchema);
User Collection
/* 1 */
{
"_id" : ObjectId("5ed267005e5d568b58cd17f7"),
"published" : true,
"name" : "Macit Mutlu Sarı",
"age" : "34",
"about" : "Ankara doğumlu ve güzel sanatlar mezunu 18 yıllık deneyimi olan bir profesyonel",
"publishedAt" : ISODate("2020-05-30T14:00:32.322Z"),
"__v" : 0
}
Replace _Id with _id
$lookup: {
from: 'users',
localField: '_id', //Replace here
foreignField: 'userId',
as: 'user'
}
Basically I have a mongodb collection called 'people'
whose schema is as follows:
people: {
name: String,
friends: [{firstName: String, lastName: String}]
}
Now, I have a very basic express application that connects to the database and successfully creates 'people' with an empty friends array.
In a secondary place in the application, a form is in place to add friends. The form takes in firstName and lastName and then POSTs with the name field also for reference to the proper people object.
What I'm having a hard time doing is creating a new friend object and then "pushing" it into the friends array.
I know that when I do this via the mongo console I use the update function with $push as my second argument after the lookup criteria, but I can't seem to find the appropriate way to get mongoose to do this.
db.people.update({name: "John"}, {$push: {friends: {firstName: "Harry", lastName: "Potter"}}});
Assuming, var friend = { firstName: 'Harry', lastName: 'Potter' };
There are two options you have:
Update the model in-memory, and save (plain javascript array.push):
person.friends.push(friend);
person.save(done);
or
PersonModel.update(
{ _id: person._id },
{ $push: { friends: friend } },
done
);
I always try and go for the first option when possible, because it'll respect more of the benefits that mongoose gives you (hooks, validation, etc.).
However, if you are doing lots of concurrent writes, you will hit race conditions where you'll end up with nasty version errors to stop you from replacing the entire model each time and losing the previous friend you added. So only go to the latter when it's absolutely necessary.
The $push operator appends a specified value to an array.
{ $push: { <field1>: <value1>, ... } }
$push adds the array field with the value as its element.
Above answer fulfils all the requirements, but I got it working by doing the following
var objFriends = { fname:"fname",lname:"lname",surname:"surname" };
People.findOneAndUpdate(
{ _id: req.body.id },
{ $push: { friends: objFriends } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
});
)
Another way to push items into array using Mongoose is- $addToSet, if you want only unique items to be pushed into array. $push operator simply adds the object to array whether or not the object is already present, while $addToSet does that only if the object is not present in the array so as not to incorporate duplicacy.
PersonModel.update(
{ _id: person._id },
{ $addToSet: { friends: friend } }
);
This will look for the object you are adding to array. If found, does nothing. If not, adds it to the array.
References:
$addToSet
MongooseArray.prototype.addToSet()
Use $push to update document and insert new value inside an array.
find:
db.getCollection('noti').find({})
result for find:
{
"_id" : ObjectId("5bc061f05a4c0511a9252e88"),
"count" : 1.0,
"color" : "green",
"icon" : "circle",
"graph" : [
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 2.0
}
],
"name" : "online visitor",
"read" : false,
"date" : ISODate("2018-10-12T08:57:20.853Z"),
"__v" : 0.0
}
update:
db.getCollection('noti').findOneAndUpdate(
{ _id: ObjectId("5bc061f05a4c0511a9252e88") },
{ $push: {
graph: {
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 3.0
}
}
})
result for update:
{
"_id" : ObjectId("5bc061f05a4c0511a9252e88"),
"count" : 1.0,
"color" : "green",
"icon" : "circle",
"graph" : [
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 2.0
},
{
"date" : ISODate("2018-10-24T08:55:13.331Z"),
"count" : 3.0
}
],
"name" : "online visitor",
"read" : false,
"date" : ISODate("2018-10-12T08:57:20.853Z"),
"__v" : 0.0
}
First I tried this code
const peopleSchema = new mongoose.Schema({
name: String,
friends: [
{
firstName: String,
lastName: String,
},
],
});
const People = mongoose.model("person", peopleSchema);
const first = new Note({
name: "Yash Salvi",
notes: [
{
firstName: "Johnny",
lastName: "Johnson",
},
],
});
first.save();
const friendNew = {
firstName: "Alice",
lastName: "Parker",
};
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
But I noticed that only first friend (i.e. Johhny Johnson) gets saved and the objective to push array element in existing array of "friends" doesn't seem to work as when I run the code , in database in only shows "First friend" and "friends" array has only one element !
So the simple solution is written below
const peopleSchema = new mongoose.Schema({
name: String,
friends: [
{
firstName: String,
lastName: String,
},
],
});
const People = mongoose.model("person", peopleSchema);
const first = new Note({
name: "Yash Salvi",
notes: [
{
firstName: "Johnny",
lastName: "Johnson",
},
],
});
first.save();
const friendNew = {
firstName: "Alice",
lastName: "Parker",
};
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
{ upsert: true }
);
Adding "{ upsert: true }" solved problem in my case and once code is saved and I run it , I see that "friends" array now has 2 elements !
The upsert = true option creates the object if it doesn't exist. default is set to false.
if it doesn't work use below snippet
People.findOneAndUpdate(
{ name: "Yash Salvi" },
{ $push: { friends: friendNew } },
).exec();
An easy way to do that is to use the following:
var John = people.findOne({name: "John"});
John.friends.push({firstName: "Harry", lastName: "Potter"});
John.save();
In my case, I did this
const eventId = event.id;
User.findByIdAndUpdate(id, { $push: { createdEvents: eventId } }).exec();
Push to nested field - use a dot notation
For anyone wondering how to push to a nested field when you have for example this Schema.
const UserModel = new mongoose.schema({
friends: {
bestFriends: [{ firstName: String, lastName: String }],
otherFriends: [{ firstName: String, lastName: String }]
}
});
You just use a dot notation, like this:
const updatedUser = await UserModel.update({_id: args._id}, {
$push: {
"friends.bestFriends": {firstName: "Ima", lastName: "Weiner"}
}
});
This is how you could push an item - official docs
const schema = Schema({ nums: [Number] });
const Model = mongoose.model('Test', schema);
const doc = await Model.create({ nums: [3, 4] });
doc.nums.push(5); // Add 5 to the end of the array
await doc.save();
// You can also pass an object with `$each` as the
// first parameter to use MongoDB's `$position`
doc.nums.push({
$each: [1, 2],
$position: 0
});
doc.nums;
// This is the my solution for this question.
// I want to add new object in worKingHours(array of objects) -->
workingHours: [
{
workingDate: Date,
entryTime: Date,
exitTime: Date,
},
],
// employeeRoutes.js
const express = require("express");
const router = express.Router();
const EmployeeController = require("../controllers/employeeController");
router
.route("/:id")
.put(EmployeeController.updateWorkingDay)
// employeeModel.js
const mongoose = require("mongoose");
const validator = require("validator");
const employeeSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, "Please enter your name"],
},
address: {
type: String,
required: [true, "Please enter your name"],
},
email: {
type: String,
unique: true,
lowercase: true,
required: [true, "Please enter your name"],
validate: [validator.isEmail, "Please provide a valid email"],
},
phone: {
type: String,
required: [true, "Please enter your name"],
},
joiningDate: {
type: Date,
required: [true, "Please Enter your joining date"],
},
workingHours: [
{
workingDate: Date,
entryTime: Date,
exitTime: Date,
},
],
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const Employee = mongoose.model("Employee", employeeSchema);
module.exports = Employee;
// employeeContoller.js
/////////////////////////// SOLUTION IS BELOW ///////////////////////////////
// This is for adding another day, entry and exit time
exports.updateWorkingDay = async (req, res) => {
const doc = await Employee.findByIdAndUpdate(req.params.id, {
$push: {
workingHours: req.body,
},
});
res.status(200).json({
status: "true",
data: { doc },
});
};
https://www.youtube.com/watch?v=gtUPPO8Re98
I ran into this issue as well. My fix was to create a child schema. See below for an example for your models.
---- Person model
const mongoose = require('mongoose');
const SingleFriend = require('./SingleFriend');
const Schema = mongoose.Schema;
const productSchema = new Schema({
friends : [SingleFriend.schema]
});
module.exports = mongoose.model('Person', personSchema);
***Important: SingleFriend.schema -> make sure to use lowercase for schema
--- Child schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const SingleFriendSchema = new Schema({
Name: String
});
module.exports = mongoose.model('SingleFriend', SingleFriendSchema);
I have a Group Collection, which is having a Reference array of Members. Two Objects are inter-connected like follow. When I am adding new members to the group the members field of Group object needed to be updated. How can I do this with a mongoose update operator.
var MemberSchema = new Schema({
name:{
type:String,
default:null
},
user_id:{
type : Schema.ObjectId,
ref : 'User',
default : null
}
});
var GroupSchema = new Schema({
name:{
type:String,
default:null
},
description:{
type:String,
default:null
},
members:[MemberSchema],
},{collection:"groups"});
Thank You in advance.
Update
I added a sample document of group.
{
"_id" : ObjectId("586a2e694467c41218b302c3"),
"members" : [
{
"_id" : ObjectId("586a2e694467c41218b302c6"),
"user_id" : ObjectId("58171d75e72bf516f92dcd4e"),
"name" : "Lakmal Kapukotuwa"
},
{
"_id" : ObjectId("586a2e694467c41218b302c5"),
"user_id" : ObjectId("5821807516325e127f59438e"),
"name" : "Prasad Perera"
},
{
"_id" : ObjectId("586a2e694467c41218b302c4"),
"user_id" : ObjectId("586263515356e908de6c899a"),
"name" : "Sadun Prasad"
}
],
"description" : "Des 1",
"name" : "My group",
"__v" : 0
}
If you are sending the new members as a list of objects with the following structure e.g.
membersListToAdd = [
{
"user_id": "58171d75e72bf516f92dcd4e",
"name": "foo"
},
{
"user_id": "5821807516325e127f59438e",
"name": "bar"
}
]
then use $push with $each modifier in an update as follows:
var query = { name: 'My Group' },
options = {},
callback = function (err, result) { console.log(result); };
Group.update(query, { $push: { members: { $each: membersListToAdd } } }, options, callback)
You are doing this wrong,
no need to have links in both collections and no need to nest models
try this instead
var Group = mongoose.model("Group", new Schema({
name: {
type:String
},
description: {
type:String
},
}));
Group.virtual("users", {
ref: "User",
localField: "_id",
foreignField: "groups"
});
var User = mongoose.model("User", new Schema({
name:{
type:String
},
groups: [{
type : Schema.ObjectId,
ref : 'Group'
}]
}));
I have the following Post and Collection documents:
// posts
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731
}
// comments
{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
},
{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}
I need to query all posts with all comments to be the following result:
{
"_id" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Post body 1",
"created_at" : 1452772748737,
"comments": [{
"_post" : ObjectId("56978d8cdbc511a81e7e2ea8"),
"body" : "Comment 1"
}]
},
{
"_id" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Post body 2",
"created_at" : 1452772759731,
"comments": [{
"_post" : ObjectId("56978d3cdbc655b81e7e2e10"),
"body" : "Comment 2"
}]
}
My schema for post and collections look the following:
// post
var PostSchema = mongoose.Schema({
},{
strict: "throw",
collection: "posts"
});
PostSchema.add({
created_at: {
type: Number,
'default': Date.now
}
});
PostSchema.add({
title: {
type: String
}
});
PostSchema.add({
body: {
type: String
}
});
// comment
var CommentSchema = mongoose.Schema({
},{
strict: "throw",
collection: "comments"
});
CommentSchema.add({
_post: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}
});
CommentSchema.add({
body: {
type: String
}
});
What is the effectively way to get the result above?
Try to do it through mongoose populate and aggregate. Sample codes as below.
var Post = mongoose.model('Post', PostSchema);
var Comment = mongoose.model('Comment', CommentSchema);
Comment.aggregate([
{$group: {_id: '$_post', comments: {$push: '$body'}}}
// ...
], function(err, result) {
if (err)
// error handling
Post.populate(result, {path: "_id"}, function(err, ret) {
if(err)
console.log(err);
else
console.log(ret);
});
});
For some reason, the $position is not doing anything in this code. Any thoughts?:
Parent.findByIdAndUpdate(
id,
{$push: {"wishList": item, $position: 0}},
{safe: true, upsert: true},
function(err, model) {
response.send("1");
}
);
Here is the schema:
/**
Defines Parent schema for mongo DB
*/
module.exports = {
name : String,
dateAdded : Date,
plHash : String,
items : [{
author : String,
name : String,
dateAdded : Date
}],
wishList : [{
author : String,
name : String,
dateAdded : Date
}],
};
Following the docs
To use the $position modifier, it must appear with the $each modifier.
You could try adding the $each modifier in your update:
Parent.findByIdAndUpdate(id,
{
$push: {
wishList: {
$each: [item], // Assuming item = {author:"foo", name:"bar", dateAdded:date }
$position: 0
}
}
}, {safe: true, upsert: true}, function(err, model){
response.send("1");
});