Node - Mongoose update method issue - javascript

I need to update and already queried document:
In memory save way works fine:
project.likers.push(profile.id);
project.set({
likes: project.likes + 1
})
await project.save();
But I read in a blog that this way is slower as compared to using mongoose update method.
Since I am using multiple queries in this endpoint, I want to use mongoose update method for the same reason.
Doesn't work
project.update({ "$push": { "likers": profile.id }, "$inc": { "likes": 1 } });
Thanks in advance!

$push : The operator appends a specified value to an array.
And there isn't command 'update'. There are two command for update and they are :
updateOne
updateMany
If you just want to update the existing field, you can use like below:
db.collectionname.updateMany({ "likers": profile.id }, { "$inc": { "likes": 1 } })
If you want more info, check out.

Related

Only update mongo document if value is bigger

I got a collection of documents in mongo DB.
One of the fields in the document is "verison" : int
I was wondering if there is a practical way to use this value to prevent update of the document when there is an attempt to update it with the same or smaller version number.
example :
my collection has a document :
{
"name": "john",
"version": 3
}
if I try to send update with :
{
"name": "rick"
"version": 3
}
It wont update it, and I will get an indication that there was no update so I can handle this in some way in my code.
I am using node.js with native mongo DB driver.
You can solve using the below Code.
db.collection.update({'version':{$gte : 3}},{$set : {name:'Abc'}},{upsert : true,new:true})

How to filter an array of subdocuments by two fields in each subdocument

I am attempting to add a help request system which allows the requestor to make only one request for help on each topic from an expert. If the expert lists multiple topics which they can help, I want to limit each requestor to one help request per topic per expert.
I am using node.js and mongoose.js with a self-hosted mongodb instance
I have tried using the $and operator to find the ._id of the expert as long as they don't already have an existing request from the same requestor on the same topic. It works for one update but after the experts document has a subdocument inserted with either the topic_id or the requestor_id the filter is applied and no expert is returned.
// Schema
ExpertSchema = new mongoose.Schema({
expert_id: String,
helpRequests: [
requestor_id: String,
topic_id: String
]
});
//query
const query = {
$and:[
{expert_id: req.body.expert_id},
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
]
};
// desired update
const update = {
$push: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
Expert.findOneAndUpdate(query, update, {new: true}, (err, expert) =>{
// handle return or error...
});
The reason you are not getting any expert is condition inside your query.
Results always returned based on the condition of your query if your condition inside query get satisfied you will get your result as simple as that.
Your query
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
you will get your expert only if requestor_id and topic_id is not exists inside helpRequests array. thats you are querying for.
Solution
As per you schema if helpRequests contains only requestor_id and topic_id then you can achieve what you desire by below query.
Expert.findOneAndUpdate(
{
expert_id: req.body.expert_id,
}, {
$addToSet: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
}, { returnNewDocument: true });

Meteor mongo: sync fields across collections

I have a meeting document like this:
{
"name":"Meeting Name",
"uuid":"NYoc2aL6",
"participants":[
{
"id":"JLKGZnfFkGvX9DHgz",
"status":"joined",
"name":"Guest 03"
},
{
// newly invited user, user hasn't logged in with invite url yet
"id":"",
"status":"invited",
"name":"email#email.com"
}
]
}
and I need to synchronize the 'name' field with the name in the Users collection. Is there an automatic way to do this, like at the database level, or am I stuck with manually updating in every place that the name is changed?
This is a pretty common pattern in Meteor. You can use the matb33:collection-hooks package to "hook" the collection update to synchronize the shared value(s). This should be done server-side of course so you don't have to worry about some of the related documents not being available to you.
Example:
Meteor.users.after.update((userId, doc, fieldNames, modifier, options)=>{
if ( fieldNames.indexOf('profile.name') > -1 ){ // the name was changed
Meetings.update({ 'participants.id': doc._id },
{ $set: { 'participants.name': doc.profile.name }},
{ multi: true });
}
});
There is no "automatic" way to do this that I know of in Mongo or Meteor. However, why not take the common fields out of your document and just link the ID? This is known as "Database Normalization", which is a process by which you remove redundant data from your tables (collections in Mongo) to prevent these sorts of problems.
This could be done 'automatically' by observing changes on the users db:
var usersCursor = Meteor.users.find();
usersCursor.observeChanges({"changed":function(id, fields){
if(fields.profile.name){
... do whatever needs to be done ...
}
}});

MongoDb: Find the generated id of the inserted child on parent save

If I have a document representing a post with comments that looks like this:
{
"_id": "579a2a71f7b5455c28a7abcb",
"title": "post 1",
"link": "www.link1.com",
"__v": 0,
"comments": [
{
"author": "Andy",
"body": "Wish I had thought of that",
"_id": "579a2a71f7b5455c28a7abcd",
"upvotes": 0
},
{
"author": "Jim",
"body": "Just a comment",
"_id": "579a2a71f7b5455c28a7abcc",
"upvotes": 0
}
],
"upvotes": 5
}
In the calling (javascript) code, I add a new comment, by pushing to the post.comments array, then save the post using .save with a callback. In the save callback, I want to get the generated _id of the new comment I just saved. How do I do this?
I've got the parent post document in the callback, of course, but that's not useful as I can't tell which comment was just inserted.
Is there another document method or an alternate form of the .save callback to deal with my situation?
Or do I have to just follow what I'd usually do and generate a unique id on the comment myself before the save?
EDITED: I'm using Mongoose, sorry, forgot to say!
You did not specifically tell, but I assume you use Mongoose because standard MongoDB will not add an _id property to subdocuments.
As mentioned in the Mongoose documentation regarding adding sub-documents, you can use the following code example:
var Parent = mongoose.model('Parent');
var parent = new Parent;
// create a comment
parent.children.push({ name: 'Liesl' });
var subdoc = parent.children[0];
console.log(subdoc) // { _id: '501d86090d371bab2c0341c5', name: 'Liesl' }
subdoc.isNew; // true
parent.save(function (err) {
if (err) return handleError(err)
console.log('Success!');
});
Instead of parent.children[0] you have to use parent.children[parent.children.length - 1] to access the inserted element, though.
I'd assume the item you pushed on the array would be the last one, but that wouldn't work in a multi user system.
Also you could make a comparison against the author and comment fields, though this seems like a lot of trouble, and with just the author and the comment text, you might not be assured a match.
Finally, you could also create the object id and assign it to the comment, then save it. You do that like this:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
That's what I would do.

Meteor.js update only one parameter instead of whole collection

I'm trying to mark message as readed using code below :
Template.FullMessage.onRendered(function () {
var id = FlowRouter.getParam('id');
Messages.update(id, {$set: {readed: true} });
});
Collection is :
"_id": "YMxYn9NodPeZqFP83",
"whatAbout": "adsfadsfasdf",
"message": "sdfadsfadfadsfasdfasdf",
"recipientId": "9ewiF8JTNp77Pmijw",
"author": "9ewiF8JTNp77Pmijw",
"createdAt": "2016-05-09T08:37:52.282Z",
"owner": "seofilms",
"readed": false
}
I expected that column "readed":"false" will be replaced with "readed":true,
but instead of it, everything in here is changing, including owner. So for instance if I will open message with user test, I will change also the owner of this message.
Why does it happens ?
Is it possible to prevent sending whole object and change it only with ID?
Thank you for any ideas.
Try this:
Messages.update({_id: id}, {$set: {readed: true} });
It should also work with only id, as you're already doing. Is there any other code that's writing to the same collection? Try to run it in console and check if it's still updating all the properties.

Categories

Resources