Push data to MongoDB without editing the whole entry in MEAN - javascript

So i've got a single page application running via MEAN Stack. I've got the following scheme/model for a Klausur (Klausur is german for exam, i want to manage exams).
var KlausurSchema = new Schema(
{
name: String,
semester: String,
krankmeldungen: [Number],
aufgaben: [{
name: String,
punkte: Number
}],
studenten: [{
matrnr: Number,
vorname: String,
nachname: String,
bewertung: [{
punkte: Number
}],
pversuch: String,
pvermerk: String,
freiverm: String,
labnr: Number,
porgnr: Number,
aenddat: String
}]
}
);
Multiple users can edit the entrys, otherwise i would just overwrite the entry. I want to have a table consisting of the "studenten" (students), is it possible to PUSH one student to my "studenten" without PUTTING (edit) the whole "Klausur", i.e. I want to push information to an array without overwriting the whole db entry!
Thanks in advance!

Please Check Docs
If you want to insert new students array. you can use below mentioned MongoDB query.
Using MongoDB
db.klausur.update(
{ name: "David" },
$addToSet: {
studenten: {
$each: [
{
matrnr: 123,
vorname: "ABC",
nachname: "XYZ",
bewertung: [{
punkte: 123
}]
},
{
matrnr: 124,
vorname: "ABCD",
nachname: "XYZA",
bewertung: [{
punkte: 1234
}]
}]
}
);
Using Mongoose
ModelName.update(
{ name: "David" },
$addToSet: {
studenten: {
$each: [
{
matrnr: 123,
vorname: "ABC",
nachname: "XYZ",
bewertung: [{
punkte: 123
}]
}]
}
);
you can also use $push instead of $addToSet. but $addToSet handle duplicates insertion issue. One more thing if you want to add single student object then just use above query without $each. for example
db.klausur.update(
{ name: "David" },
$addToSet: {
studenten: {
matrnr: 123,
vorname: "ABC",
nachname: "XYZ",
bewertung: [{
punkte: 123
}]
}
}
);

Pass an object to be updated to native mongoDB update query.
The pseudo query will be,
db.model.update(selector, objectToUpsert);
db.student.update(
{ name: "David" },
{
name: "Will",
marks: 75,
grade: A
},
{ upsert: true }
)

First find Klausur=exam say
Klausur.findOne({_id:sample_id}).exec(function (error, closure){
closure.students.push(newstudentobject);
closure.save();
})

Related

MongoDb - Delete Json object from array

I would like to delete an object from a JSON objects array. Here is the schema
qualifications: {
Experience: [{
title: String,
companyName: String,
location: String,
years: Number
}],
Education:[ {
school: String,
years: Number,
}],
Licences: [String],
Honnors: [String],
}
For example how can I delete the object whose key is "school": "harvard university" ?
What i tried is
const user = await User.findOneAndUpdate(
{ _id: req.body.userid },
{
$pull: {
qualifications: {
Education: {
school: "harvard university",
}
},
},
}
);
But unfortunatelly it doesn't get deleted from the database. what is wrong?
can you try:
await User.update({ _id: req.body.userid },
{
"$pull": {
"qualifications.Education": {
"school": "harvard university",
},
},
});
qualifications is an object, thus you can't use the $pull operator which requires an array field. Instead, you need the (.) dot notation to update the nested Education array: qualifications.Education.
const user = await User.findOneAndUpdate(
{ _id: req.body.userid },
{
$pull: {
"qualifications.Education": {
school: "harvard university"
}
}
})
Demo # Mongo Playground
Updated
From your error message, it seems qualifications is an array instead of an object. Your schema should be as below:
qualifications: [{
Experience: [{
title: String,
companyName: String,
location: String,
years: Number
}],
Education:[ {
school: String,
years: Number,
}],
Licences: [String],
Honnors: [String],
}]
To remove the object from the nested arrays, the below query aims to remove all the objects with school: "harvard university" from the Education array, for the all objects in the qualifications array,
const user = await User.findOneAndUpdate(
{
_id: req.body.userid,
"qualifications.Education.school": "harvard university"
},
{
$pull: {
"qualifications.$[].Education": {
school: "harvard university"
}
}
})
Demo (remove object from nested arrays) # Mongo Playground

Edit multiple objects in array using mongoose (MongoDB)

So I tried several ways, but I can't, I can modify several objects with the same key but I can't modify any with different keys, if anyone can help me is quite a complex problem
{
id: 123,
"infos": [
{ name: 'Joe', value: 'Disabled', id: 0 },
{ name: 'Adam', value: 'Enabled', id: 0 }
]
};
In my database I have a collection with an array and several objects inside which gives this.
I want to modify these objects, filter by their name and modify the value.
To give you a better example, my site returns me an object with the new data, and I want to modify the database object with the new object, without clearing the array, the name key never changes.
const object = [
{ name: 'Joe', value: 'Hey', id: 1 },
{ name: 'Adam', value: 'None', id: 1 }
];
for(const obj in object) {
Schema.findOneAndUpdate({ id: 123 }, {
$set: {
[`infos.${obj}.value`]: "Test"
}
})
}
This code works but it is not optimized, it makes several requests, I would like to do everything in one request, and also it doesn't update the id, only the value.
If anyone can help me that would be great, I've looked everywhere and can't find anything
My schema structure
new Schema({
id: { "type": String, "required": true, "unique": true },
infos: []
})
I use the $addToSet method to insert objects into the infos array
Try This :
db.collection.update({
id: 123,
},
{
$set: {
"infos.$[x].value": "Value",
"infos.$[x].name": "User"
}
},
{
arrayFilters: [
{
"x.id": {
$in: [
1
]
}
},
],
multi: true
})
The all positional $[] operator acts as a placeholder for all elements in the array field.
In $in you can use dynamic array of id.
Ex :
const ids = [1,2,..n]
db.collection.update(
//Same code as it is...
{
arrayFilters: [
{
"x.id": {
$in: ids
}
},
],
multi: true
})
MongoPlayGround Link : https://mongoplayground.net/p/Tuz831lkPqk
Maybe you look for something like this:
db.collection.update({},
{
$set: {
"infos.$[x].value": "test1",
"infos.$[x].id": 10,
"infos.$[y].value": "test2",
"infos.$[y].id": 20
}
},
{
arrayFilters: [
{
"x.name": "Adam"
},
{
"y.name": "Joe"
}
],
multi: true
})
Explained:
You define arrayFilters for all names in objects you have and update the values & id in all documents ...
playground

Mongoose - renaming object key within array

I have this one schema
{
_id: "123456",
id: "123",
inventory: [
{
id: "foo",
count: 0
},
{
id: "bar",
count: 3
}
]
}
I wanted every "count" keys in the inventory array to be "price" which will look like this at the end:
{
_id: "123456",
id: "123",
inventory: [
{
id: "foo",
price: 0
},
{
id: "bar",
price: 3
}
]
}
And I've tried this
Model.updateOne({ id: "123" }, { $unset: { inventory: [{ count: 1 }] } } )
But it seems to be deleting the "inventory" field itself
The first thing here is to try to use $rename but how the docs explain:
$rename does not work if these fields are in array elements.
So is necessary to look for another method. So you can use this update with aggregation query:
This query uses mainly $map, $arrayToObject and $objectToArray. The trick here is:
Create a new field called inventory (overwrite existing one)
Iterate over every value of the array with $map, and then for each object in the array use $objectToArray to create an array and also iterate over that second array using again $map.
Into this second iteration create fields k and v. Field v will be the same (you don't want to change the value, only the key). And for field k you have to change only the one whose match with your condition, i.e. only change from count to price. If this condition is not matched then the key remain.
db.collection.update({},
[
{
$set: {
inventory: {
$map: {
input: "$inventory",
in: {
$arrayToObject: {
$map: {
input: {$objectToArray: "$$this"},
in: {
k: {
$cond: [
{
$eq: ["$$this.k","count"]
},
"price",
"$$this.k"
]
},
v: "$$this.v"
}
}
}
}
}
}
}
}
])
Example here

How To Push mutiple array elements into mongod by mongoose

There is a mongodb Schema which include these field, its type is array
......
orderlist: [
{
id: String,
price: Number,
photo: String,
name: String,
num: Number
}
]
......
The frontend post me the data such as this,this array has lots of array elements
goodslist:[
{
goodsid: '10001',
goodsprice: 20,
goodsphoto: '/goodsimg/upload_1843.jpg',
goodsname: 'goods1',
goodsnum: 2
},
{
goodsid: '10002',
goodsprice: 30,
goodsphoto: '/goodsimg/upload_1845.jpg',
goodsname: 'goods2',
goodsnum: 4
},
........(etc)
]
what can I do to push this 'goodslist' data into 'orderlist' field by mongoose without changing mongodb field, thanks
You must use mongoose virtuals to achieve this issue.
Your schema must like this:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const collectionName = 'orderlist';
const OrderSchema = new Schema({
id: String,
price: Number,
photo: String,
name: String,
num: Number
}, { minimize: false });
const OrderlistSchema = new Schema({
orderList: [OrderSchema]
}, { minimize: false, toJSON: { virtuals: true } });
OrderlistSchema.virtual('goodslist').
get(function () {
return this.orderList.map(order => ({
goodsid: order.id,
goodsprice: order.price,
goodsphoto: order.photo,
goodsname: order.name,
goodsnum: order.num
}))
}).
set(function (v) {
this.set({
orderList: v.map(good => ({
id: good.goodsid,
price: good.goodsprice,
photo: good.goodsphoto,
name: good.goodsname,
num: good.goodsnum
}))
});
});
module.exports = mongoose.model('Orderlist', OrderlistSchema, collectionName);
goodslist is virtual field here.
With this schema, you can set order field with your format without changing anything in mongodb.
Example posting document:
{
"goodslist": [
{
"goodsid": 2,
"goodsprice": 200,
"goodsphoto": "photo2",
"goodsname": "name2",
"goodsnum": 1234
}
]
}
you can also get order data in goodlist format
{
"_id": "5e9d8c0e27c7a813840c9ff0",
"orderList": [
{
"_id": "5e9d8c0e27c7a813840c9ff1",
"id": "2",
"price": 200,
"photo": "photo2",
"name": "name2",
"num": 1234
}
],
"__v": 0,
"goodslist": [
{
"goodsid": "2",
"goodsprice": 200,
"goodsphoto": "photo2",
"goodsname": "name2",
"goodsnum": 1234
}
],
"id": "5e9d8c0e27c7a813840c9ff0"
}
It going to be something like the following:
//Update order | create if does not exist
orderDB.updateOne({ _id: 'xxxx' }, {
//Push the list into order array
$push: {
orderlist: [{
id: goodlist[0][0],
price: goodlist[0][1],
photo: goodlist[0][2],
name: goodlist[0][3],
num: goodlist[0][4],
}]
}
//Upsert => update / create
}, { upsert: true })
However, you might need to loop through the goodslist.

Mongoose & getting average of ratings in a collection?

I have a NodeJS based application using Mongoose. I am wanting to create a response where there values are an average of ratings provided by people who have responded to a questionnaire, for each question asked.
My Schema looks as follows:
const questionnaireResultSchema = new Schema({
user: { type: ObjectId, ref: 'User' },
questionnaire: { type: ObjectId, ref: 'Questionnaire' },
rating: [{
id: Number,
question: String,
value: Number
}]
},{
timestamps: true
}).index({user: 1, questionnaire: 1}, {unique: true});
I have looked at the Mongoose aggregator operator, but I am not sure how I would apply it to my case. Pseudo code would look as follows:
Find all questionnaire results for questionnaire of id xyz
Provide a result where the ratings for each questionnaire result has be averaged
For example the response would look as follows:
[{
id: 1,
question: 'How strongly do you feel about candidate A?',
value: 12 // averaged value
},{
id: 2,
question: 'How strongly do you think we should change the sky color to green?',
value: 31 // averaged value
},{
id: 3,
question: 'How strongly do you think your answers count?',
value: 20 // averaged value
}]
I have tried:
QuestionnaireResultSchema.aggregate([{
$match: {
questionnaire: questionnaire
}},
{$project: {
scores: { $avg: '$scores'}
}}
]);
This just provides the JSON:
[{
"_id": "57bbd4b495407f6145b3ba9f",
"scores": null
}]
A sample document in a collection would like:
{
user: ObjectId("57bca30536e376c653f439bb")
questionnaire: ObjectId("37bca0feedb0bc470353ab")
scores: [{
id: 1,
question: 'How strongly do you feel about candidate A?',
value: 3
},{
id: 2,
question: 'How strongly do you think we should change the sky color to green?',
value: 4 // averaged value
},{
id: 3,
question: 'How strongly do you think your answers count?',
value: 5 // averaged value
}]
}
While I could calculate the averages myself, if Mongoose provides the functionality, I would rather leverage that.
Any help would be appreciated.
This should work for your aggregation pipeline:
[
{
$match: {
questionnaire: ObjectId("37bca0feedb0bc470353ab")
}
},
{
$unwind: "$rating"
},
{
$group:{
_id:{
"rating_id": "$rating.id",
"question": "$rating.question",
},
avg_rating: {$avg:"rating.value"}
}
},
{
$project:{
"id": "$_id.rating_id",
"question": "$_id.question",
"avg_rating": "$avg_rating"
}
}
]
Although your sample doc has "scores" instead of "rating" in which case you'd use:
[
{
$match: {
questionnaire: ObjectId("237bca0feedb0bc470353aba")
}
},
{
$unwind: "$scores"
},
{
$group:{
_id:{
"rating_id": "$scores.id",
"question": "$scores.question",
},
avg_rating: {$avg:"scores.value"}
}
},
{
$project:{
"id": "$_id.rating_id",
"question": "$_id.question",
"avg_rating": "$avg_rating"
}
}
]
Also, some of the ObjectId's that you are using are not valid. I'm assuming those are just stubbed.

Categories

Resources