Create Rating if Not Rated Yet, Update User Rating if Rating Exists - node.js

I am creating a user experience where a user will be able to rate items from different vendors. My initial thought is for each User schema to have an array which stores all the items that the user has rated. The rated item would include the unique vendor item ID and a numerical rating value.
User Model
const userSchema = new mongoose.Schema({
...
userType: String,
ratedItems: Array,
...
});
Controller
exports.postUpdateRatedItem = (req, res, next) => {
User.findById(req.user.id, (err, user) => {
if (err) { return next(err); }
user.update(
{ $push: {ratedItems : {
vendorItem : req.body.itemID,
rating : req.body.rating
}}},
function (err) {
res.send(200);
});
});
}
Current Output
{
"_id" : ObjectId("5c91869a71ece20551fd6aed"),
"userType" : "participant",
"ratedItems" : [
{ "vendorItem" : "5c9bdd524a0dfa753e08a0a4", "rating" : "3" },
{ "vendorItem" : "5c9bdd524a0dfa753e08a0a4", "rating" : "6" }
]
}
This approach works great in adding new object to the array, but only adds and does not update. Instead, every time a user updates a rating, a new object is added to the array. What approach would allow to check for the unique vendorItem id? How do I go about checking the user rated items? If found, update the rating value, if not found, push to the array.
Thank you in advance, still learning MongoDB/Mongoose.
Edit
Below is what I expect the outcome. For each object in the array, the 'rating' is updated when the user changes the rating. The ratedItems array will eventually have many many vendorItem with unique IDs and ratings.
Expected Output
{
"_id" : ObjectId("5c91869a71ece20551fd6aed"),
"userType" : "participant",
"ratedItems" : [
{ "vendorItem" : "5c9bdd524a0dfa753e08a0a4", "rating" : "6" },
// additional rated items all with unique IDs
{ "vendorItem" : "5c9bcc14d5161c38a4581e28", "rating" : "2" },
{ "vendorItem" : "5c9407d143cd0f20d758acdb", "rating" : "11" }
]
}

It sounds like you are looking for "upsert" functionality. The Mongoose model API provides findByIdAndUpdate and other similar methods for this.
Make sure you set the new and upsert options to true. This will create the object if it doesn't exist and return the modified document if it is updated.
Your use case would look something like this:
const update = {
$push: {
ratedItems: {
vendorItem: req.body.itemID,
rating: req.body.rating
}
}
};
const options = {'new': true, upsert: true};
User.findByIdAndUpdate(req.user.id, update, options, function(err, user) {
// ...
});

Related

Mongoose/Node.js : The positional operator did not find the match needed from the query

I try to update a MongoDB collection with Mongoose & NodeJS but I have the following error who appears sometimes, but not all the times :
MongoServerError: Plan executor error during findAndModify :: caused by :: The positional operator did not find the match needed from the query.
Here is my code:
NodeJS
try {
this.myArrayUpdate = { 'description': 'test' };
this.update = { $set:
{ 'myArray.$': this.myArrayUpdate }
};
const cardId = ObjectId("61d6e320520fac65775a7ba8")
const userId = ObjectId("61a8e433648e1963ae7358be");
const filter = {
userId: userId,
'myArray._id': cardId,
};
const options = {
upsert: true,
new: true
};
MyCollection
.findOneAndUpdate(
filter,
this.update,
options,
)
.then ( () => {
return res.status(204).json({ message: 'MyCollection updated ' });
});
} catch (error) {
throw error;
}
Mongodb
{
"_id" : ObjectId("61a8e433648e1963ae7358be"),
"myArray" : [
{
"description" : "test",
"starting" : null,
"ending" : null,
"_id" : ObjectId("61d6e320520fac65775a7ba8")
},
{
"description" : "test 2",
"starting" : null,
"ending" : null,
"_id" : ObjectId("61d6e320520fac657812991a")
},
],
}
If found a part of an answer, each time I update my subdocument array, the _id change so it's why there is an error, if I update my page no more error !
Is there a possibility to not create a new _id when updating a subdocument array ?
Thanks for your answer,
David
The document you provided doesn't have an userId field, so your filter won't match the document. The field is called _id:
const filter = {
_id: userId,
'myArray._id': cardId,
};
Also, the way the update document is written, the entire matched element in myArray will be overwritten, but I assume you only want to update description. It would look like this:
this.update = { $set:
{ 'myArray.$.description': 'test' }
};
This is an example of updating a document in an array. Here's a working example in Mongo playground.

Prevent mongoose "Model.updateOne" from updating ObjectId(_id) of the model when using "$set"

I'm updating the age and name of a character with a specific _id from an array of characters that is inside a document of model Drama.
The document I'm working with:-
{
"_id" : ObjectId("619d44d2ec2ca20ca0404b5a"),
"characters" : [
{
"_id" : ObjectId("619fdac5a03c8b10d0b8b13c"),
"age" : "23",
"name" : "Vinay",
},
{
"_id" : ObjectId("619fe1d53810a130207a409d"),
"age" : "25",
"name" : "Raghu",
},
{
"_id" : ObjectId("619fe1d53810a130207a502v"),
"age" : "27",
"name" : "Teju",
}
],
}
So to update the character Raghu I did this:-
const characterObj = {
age: "26",
name: "Dr. Raghu",
};
Drama.updateOne(
{ _id: req.drama._id, "characters._id": characterId },
{
$set: {
"characters.$": characterObj,
},
},
function(err, foundlist) {
if (err) {
console.log(err);
} else {
console.log("Update completed");
}
}
);
// req.drama._id is ObjectId("619d44d2ec2ca20ca0404b5a")
// characterId is ObjectId("619fe1d53810a130207a409d")
This updated the character but it also assigned a new ObjectId to the _id field of the character. So, I'm looking for ways on how to prevent the _id update.
Also, I know I can set the individual fields of character instead of assigning a whole new object to prevent that but it will be very tedious if my character's object has a lot of fields.
//Not looking to do it this way
$set: {
"characters.$.age": characterObj.age,
"characters.$.name": characterObj.name,
},
Thanks.
I found something here, just pre define a schema (a blueprint in a way) that affects the id
var subSchema = mongoose.Schema({
//your subschema content
},{ _id : false });
Stop Mongoose from creating _id property for sub-document array items
Or I would say, when you create a character assign it a custom id from the start, that way it will retain that id throughout.
I'm leaving this question open as I would still like to see a simpler approach. But for now, I did find one easy alternative solution for this issue which I'm will be using for some time now until I find a more direct approach.
In short - Deep merge the new object in the old object using lodash and then use the new merged object to set field value.
For example, let's update the character Raghu from my question document:-
First install lodash(Required for deep merging objects) using npm:
$ npm i -g npm
$ npm i --save lodash
Import lodash:
const _ = require("lodash");
Now update the character Raghu like this:-
const newCharacterObj = {
age: "26",
name: "Dr. Raghu",
};
Drama.findById(
{ _id: req.drama._id, "characters._id": characterId },
"characters.$",
function(err, dramaDocWithSpecificCharacter) {
console.log(dramaDocWithSpecificCharacter);
// ↓↓↓ console would log ↓↓↓
// {
// "_id" : ObjectId("619d44d2ec2ca20ca0404b5a"),
// "characters" : [
// {
// "_id" : ObjectId("619fe1d53810a130207a409d"),
// "age" : "25",
// "name" : "Raghu",
// }
// ],
// }
const oldCharacterObj = dramaDocWithSpecificCharacter.characters[0];
const mergedCharacterObjs = _.merge(oldCharacterObj, newCharacterObj);
// _.merge() returns a deep merged object
console.log(mergedCharacterObjs);
// ↓↓↓ console would log ↓↓↓
// {
// _id: 619fe1d53810a130207a409d,
// age: "26",
// name: "Dr. Raghu",
// };
Drama.updateOne(
{ _id: req.drama._id, "characters._id": characterId },
{
$set: {
"characters.$": mergedCharacterObjs,
},
},
function(err, foundlist) {
if (err) {
console.log(err);
} else {
console.log("Update completed");
}
}
);
}
);
// req.drama._id is ObjectId("619d44d2ec2ca20ca0404b5a")
// characterId is ObjectId("619fe1d53810a130207a409d")
Note: We can also use the native Object.assign() or … (spread operator) to merge objects but the downside of it is that it doesn’t merge nested objects which could cause issues if you later decide to add nested objects without making changes for deep merge.
You can pass your payload or request body like this if we provide _id it will prevent update to nested document
"characters" : [
{
"_id" : "619fdac5a03c8b10d0b8b13c",
"age" : "updated value",
"name" : "updated value",
}, {
"_id" : "619fe1d53810a130207a409d",
"age" : "updated value",
"name" : "updated value",
}, {
"_id" : "619fe1d53810a130207a502v",
"age" : "updated value",
"name" : "updated value",
}
],
It works for me for bulk update in array object

How to find a record using dot notation & update the value in a schema using mongoose

I am using mongoose to perform CRUD operation on my db. This is how my model looks.
var EmployeeSchema = new Schema({
name: String,
description: {
type: String,
default: 'No description'
},
department: [],
lastUpdated: {
type: Date,
default: Date.now
}
});
The department can contains array of object like this.
[
{
"id" : "55ba28f680dec4383eeebf97",
"text" : "Sales",
"topParentId" : "55ba28f680dec4383eeebf8b",
"topParentText" : "XYZ"
},
{
"id" : "55ba28f680dec4383eeebf98",
"text" : "IT",
"topParentId" : "55ba28f680dec4383eeebf8b",
"topParentText" : "XYZ"
},
{
"id" : "55ba28f680dec4383eeebf94",
"text" : "Marketing",
"topParentId" : "55ba28f680dec4383eeebccc",
"topParentText" : "ABC"
}
]
Now I need to find all the employee where department.id = '55ba28f680dec4383eeebf94' and then I need to update the text of the object.
Employee.find({'department.id': '55ba28f680dec4383eeebf94'}, function(err, Employees) {
_.each(Employees, function (emp) {
_.each(emp.department, function (dept) {
if(dept.id === '55ba28f680dec4383eeebf94'){
dept.text = 'XXXXX'; // How to update the employee to save the updated text
}
});
});
});
What is the right way to save the employee with updated text for that department?
Iterating is code is not a "sharp" way to do this. It is better to use the MongoDB update operators, especially since there is no schema defined for the array items here, so no rules to worry about:
Employee.update(
{'department.id': '55ba28f680dec4383eeebf94'},
{ "$set": { "department.$.text": "XXXXX" },
function(err,numAffected) {
// handling in here
}
);
The $set is the important part, otherwise you overwrite the whole object. As is the positional $ operator in the statement, so only the matched ( queried item in the array ) index is updated.
Also see .find**AndUpdate() variants for a way to return the modified object.
I think you can use the update model:
Employee.update({department.id: '55ba28f680dec4383eeebf94'}, {department.text: 'XXXXX'}, {multi: true},
function(err, num) {
console.log("updated "+num);
}
);
First object is the query, what to find: {department.id: '55ba28f680dec4383eeebf94'}, the second one is the update, what to update: {department.text: 'XXXXX'} and the third one is the options to pass to the update, multi means update every records you find: {multi: true}

Delete item from MongoDB with Nodejs

I'm trying to delete a user id from all collections that have a reference to it. I'm bringing a user id across from the form and want to remove every reference to it in every business collection. I know the below query doesn't work but it shows my current approach.
db.collection('business', function (err, allBus){
allBus.update({}, { $pull: {followers: { userID } } } );
});
Here is my data, any ideas?
{
"_id" : ObjectId("55355d0ab063708c0b73809e"),
"address" : "Donegal",
"businessName" : "burkes shoes",
"email" : "info#burkes.ie",
"followers" : [
ObjectId("55300f5208224af428d1beaf"),
ObjectId("553129666252d2fc0a4634e4")
],
"gpsLat" : "55.1763595",
"gpsLong" : "-7.7923",
"homeDomain" : "www.burkes.ie",
"imgpath" : "\\images\\uploads\\57461Burkes_logo_1429560586607.jpg",
"password" : "1",
"role" : "business"
}
If userID is a string you will need to cast it first to ObjectID before using it in your query. Something like this should do the magic:
var ObjectID = require("mongodb").ObjectID,
userID = new ObjectId("55300f5208224af428d1beaf");
/*
if userID is a string then this will work
var userID = new ObjectId(userID);
*/
db.business.update(
{"followers": userID},
{
"$pull": { "followers": userID }
},
{ multi: true }
);
The query above will have better performance than an update without a query as it first filters documents that have in their followers array an element with the userID value and then updates the matched documents by pulling the ObjectID value from the array.

Mongoose/Mongo faster to have separate collections?

Currently I have a schema USERS with a sub doc VIEWED
As a 'user' views other users, their ID gets logged in the viewers sub doc
So when viewing, technically this gets all the users, then filters that through all the viewed users [for any given user]. So you get a list of unique/fresh users.
My method is currently fetching the list of users - Query 1
Then its fetching the list of viewed users (for a given user) - Query 2
Then using array.filter function to get a list of new users.
(using async parallel for those queries)
Question is, would it be faster to just have a separate document/collection that stores a list of viewed users for any given user. e.g:
{
userID: 1002,
viewedID: 9112
},
{
userID: 1002,
viewedID: 9222
},
{
userID: 1002,
viewedID: 9332
}
Is it possible for me to some how do a query that gets me a fresh list of users, so i don't have to do the computation myself. i.e let mongo do all the work.
edit, adding code to make it more clear
var ViewedSchema = new Schema({
coupleId: {type: Number, required: true}
});
var UserSchema = new Schema({
name : { type: String, trim: true, required: true}
, partnerId : { type: Number}
, viewed : [ViewedSchema]
});
code to view partners/users that have not been viewed before
async.parallel([
function(callback) {
//gets all the users/partners
User.find({}, function(err, users) {
var allPartners = [];
users.forEach(function(user){
if(allPartners.indexOf(user.partnerId) == -1) {
allPartners.push(user.partnerId);
}
});
callback(null, allPartners);
});
},
function(callback) {
//gets all the users/partners i have already viewed
var votedPartners = [];
User.findById(id, function(err, user) {
user.viewed.forEach(function(user){
votedPartners.push(user.coupleId);
});
callback(null, votedPartners);
});
}
],
function(err, result) {
//gets the differences between the 2 arrays
function exists(element) {
return (result[1].indexOf(element) == -1);
}
var showPartners = result[0].filter(exists);
User.find({partnerId: showPartners[0]}, function(err, user){
var json = {objects: user};
res.render('index', json);
});
});
};
I'm not sure what you mean by fresh or new users, exactly, but have you loked at the distinct() command? You can use it to get all the unique viewed user IDs for all the users in the collection, which is what it sounds like you want to do. See
http://docs.mongodb.org/manual/reference/method/db.collection.distinct/
From the documentation:
Return an array of the distinct values of the field sku in the subdocument item from all documents in the orders collection:
db.orders.distinct( 'item.sku' )
If you give an example of your current document schema, I could try to write the exact query for you.
Edit: You can use $nin to find the userIds that are not in a given list. Here is an example I set up in my local Mongo:
> db.dating.insert({"userId":100,"viewedId":["200","201"]})
> db.dating.findOne()
{
"_id" : ObjectId("5398d81799b228e88aef2441"),
"userId" : 100,
"viewedId" : [
"200",
"201"
]
}
> db.dating.insert({"userId":200,"viewedId":[""]})
> db.dating.insert({"userId":201,"viewedId":[""]})
> db.dating.insert({"userId":202,"viewedId":[""]})
> db.dating.insert({"userId":203,"viewedId":[""]})
> db.dating.insert({"userId":204,"viewedId":[""]})
> db.dating.insert({"userId":205,"viewedId":[""]})
> db.dating.find({"userId":{$nin: [200,201]}})
{ "_id" : ObjectId("5398d81799b228e88aef2441"), "userId" : 100, "viewedId" : [ "
200", "201" ] }
{ "_id" : ObjectId("5398d84099b228e88aef2444"), "userId" : 202, "viewedId" : [ "
" ] }
{ "_id" : ObjectId("5398d84799b228e88aef2445"), "userId" : 203, "viewedId" : [ "
" ] }
{ "_id" : ObjectId("5398d85699b228e88aef2446"), "userId" : 204, "viewedId" : [ "
" ] }
{ "_id" : ObjectId("5398d85c99b228e88aef2447"), "userId" : 205, "viewedId" : [ "
" ] }

Resources