I have objects in the database, they have an array of rating objects, I need if the user’s ID is already in these objects, update the rate, and if not, add a new object to the array with the rate and user ID
Document Image :
Code :
router.post('/rating', (req, res) => {
// console.log(req.body)
let ID = 'f58482b1-ae3a-4d8a-b53b-ede80fe1e225';
// let user = '5e094d988ddbe02020e13879';
let user = 'asdadasdasd';
Habalka.find({
_id: ID
},
{rating: {$elemMatch: {user}}})
.then(res => {
if (res[0].rating.length) {
// Habalka.updateOne(
// {
// _id: ID,
// 'rating.user': user
// },
// {$inc:{"rating.$.rate.1":10}}
// )
// .then(resUpdate => {
// console.log(resUpdate)
// })
// **HERE I need to update rate if the user is already there**
} else {
// **HERE if there is no user, I need to insert an object with it and rate**
}
});
// console.log(req)
});
JSON Doc :
{
"_id":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"bio":{
"firstname":"Лена",
"lastname":"фыв",
"middlename":"",
"company":"вв"
},
"files":[
{
"_id":"2e4e40c7-4df6-4974-8d16-bb24cd8134d6",
"destination":"./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"filename":"2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"path":"uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"folder":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"info":{
"size":20805727,
"mimetype":"audio/mp3",
"encoding":"7bit",
"originalname":"Ахуевший Ленусик (Банк русский стандарт). Выпуск #5..mp3",
"fieldname":"selectedFile"
},
"userId":"5e05da745b21e61ccc84a892",
"date":"2019-12-27T10:19:12.213Z",
"guessId":{
"f58482b1-ae3a-4d8a-b53b-ede80fe1e225":[
"5e05da745b21e61ccc84a892",
"5e094d988ddbe02020e13879"
],
"b7d00dea-c872-43f4-b193-8454bef5cf85":[
]
}
},
{
"_id":"81b94dea-ece6-421c-b68a-0aa59332cd0d",
"destination":"./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"filename":"81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
"path":"uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
"folder":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"info":{
"size":13515683,
"mimetype":"audio/mp3",
"encoding":"7bit",
"originalname":"Выпуск #75 Попрошайка НСВ..mp3",
"fieldname":"selectedFile"
},
"userId":"5e05da745b21e61ccc84a892",
"date":"2019-12-27T10:25:37.710Z",
"guessId":{
"b7d00dea-c872-43f4-b193-8454bef5cf85":[
"5e05da745b21e61ccc84a892",
"5e094d988ddbe02020e13879"
],
"f58482b1-ae3a-4d8a-b53b-ede80fe1e225":[
]
}
}
],
"date":"2019-12-27T10:19:12.213Z",
"__v":1,
"rating":[
{
"rate":4,
"user":"5e094d988ddbe02020e13879"
},
{
"rate":3,
"user":"asdadasdasd"
}
]
}
You can try .bulkWrite() :
Habalka.bulkWrite([{
updateOne: {
"filter": { "rating.user": "asdadasdasd" },
"update": { $set: { "rating.$.rate": 13 } } // Will update rate
}
},
{
updateOne: {
"filter": { "rating.user": { $ne: "asdadasdasd" } },
"update": { $push: { "rating": { "user": "asdadasdasd", "rate": 13 } } } // will push new object to rating
}
}])
From the above two updateOne
's only one will write to array rating
at any given case.
this is a good solution, but as you can see I can have several objects, you can look at the screen there two with the id "b7d00dea-c872-43f4-b193-8454bef5cf85" and "f58482b1-ae3a-4d8a-b53b-ede80fe1e225", and is added immediately in two, how to make sure that it is added to only one selected
and you can tell how to get a new object, usually I get it with "then" but "then" doesn’t return it
ok, I added "_id": ID, to filter, but still don't understand how to get knew object
@СтасРябцев : bulkwrite doesn’t return the document that’s the trade off you’ll be looking at when you’ve to avoid multiple db calls..
omg (((( then I should return value that I get from req.body.value and insert it in frontend ?