Combine $push and save in mongoDB - javascript

Writing my pet application I met some problem. I'm using nodejs and mongojs library to work with mongo.
I wrote code:
db.users.findOne({_id: ObjectId(id)}, function (err, doc) {
if (err) {
res.status(500).send('Can not find name for user.');
return;
}
userName = doc.userName;
db.notes.findOne({userId: id}, function (err, doc) {
if (!doc) {
db.notes.insert({
userId: id,
userName: userName,
notes: [req.body]
}, function (err, doc) {
if (err) {
res.status(500).send('Server error.');
return;
}
res.json(doc);
});
} else {
db.notes.update({
userId: id
}, {
$push: {
'notes': req.body
}
},
function (err, doc) {
if (err) {
res.status(500).send('Server error.');
return;
}
res.json(doc);
});
}
if (err) {
res.status(500).send('Server error.');
return;
}
});
});
Now it looks like callback hell for me. I try to modify my code to make it more readable and use save method:
users.findOne({_id: makeObjectId(id)}, function (err, doc) {
if (err) {
res.status(500).send('Can not find name for user.');
return;
}
userName = doc.userName;
notes.save({
userId: id,
userName: userName
}, {
$push: {
'notes': req.body
}
}, function (err, doc) {
if (err) {
return res.status(500).send('Error during inserting the note');
}
res.status(200).send(doc);
});
});
Now it looks more readable but doesn't work. Is it possible in MongoDB to combine save method (to make it possible to modify document if it exists and create new if not) and $push for one of parameters?
Thanks in advance.

Bingo!!!
notes.update({
userId: id,
}, {
$set: {
userId: id,
userName: userName
},
$push: {
notes: req.body
}
}, {
upsert: true
}, function (err, doc) {
if (err) {
return res.status(500).send('Error during inserting the note');
}
res.status(200).send(doc);
});
Thanks to Blakes Seven

Related

MongoDB/Mongoose $pull an Object in an Array where _id of the Object is matching

I have this Schema here
Consider the likedTours which is an Array of Objects (Tours) (ignore position 0).
I want to pull any Objects where the _id of a Tour matches the critiria.
Adding a new Tour upon liking a tour is okay, but on unlike I don't know how to pull that item out.
Here is my function in the Controller in the Node.JS backend
const unlikeTour = async (req, res) => {
try {
TourDB.Tour.findOneAndUpdate(
{ _id: req.params.tourid },
{
$pull: { likedUsers: req.userID },
$inc: { likes: -1 },
}
).exec(async (err, docs) => {
if (!err) {
try {
await UserDB.User.findOneAndUpdate(
{ _id: req.userID },
{ $pull: { 'likedTours._id': docs._id } } //Here I need help
).exec()
return res.status(200).send({ successMessage: 'Tour successfully unliked' })
} catch (err) {
return res.status(500).send({ errorMessage: 'User not found' })
}
} else {
return res.status(500).send({ errorMessage: 'Tour not found' })
}
})
} catch (err) {
return res.status(500).send({ errorMessage: err })
}
}
This method looks for a tour and update it by pulling out the userID and decrement the likes count by -1.
And then I try to find in the UserDB that tour in the likedTours and tried to pull but it doesn't not work like that.
Thanks in advance
you can update as
await UserDB.User.findOneAndUpdate(
{ _id: req.userID },
{ $pull: { likedTours: { _id: docs._id } } } //Here I need help
).exec();
reference: https://docs.mongodb.com/manual/reference/operator/update/pull/

the nested function is not performed NodeJS, Mongoose

I have created a function that is looking for any reservation document with matching _id, if exists is deleted, and next the Book(findById) function is performed which is looking for any books using function(findOneAndUpdate), if exists is added value to quantity +1.But i don't know why but the function does not want to be done.
router.post('/cancel-reservation', (req, res) => {
var reservation = req.body.reservation;
Reservation.findByIdAndRemove(reservation._id, function (err) {
if (err) throw err;
else {
Book.findById(reservation.idBook, (err, book) => {
if (err) throw err;
if (book == null) {
//////////// //*WORKING *////////////////
let bookObj = {
name: reservation.nameBook,
description: reservation.description,
publishingHouse: reservation.publishingHouse,
quantity: 1,
year: reservation.year,
sites: reservation.sites
};
var book = new Book(bookObj);
book.save()
.then(function (book) {
res.json(book)
})
.catch((err) => {
res.json('not saved')
})
//////////// //*WORKING *////////////////
} else if (book) {
// findOneAndUpdate
//////////// //*HERE IS PROBLEM *////////////////
book.quantity = book.quantity+1;
console.log(book._id)
Book.findOneAndUpdate({ _id: book._id },
{ $set: { quantity : book.quantity } }, { upsert: true }),
((err, complete) => {
if(err) console.log('errrrrr');
else {
console.log(complete)
console.log('complete')
res.json(complete)
}
})
}
});
}
});
});
this problem may be related to the fact that the findOneAndUpdate function is nested in findById?
I think that you have an extra parentheses on the findOneAndUpdate
Book.findOneAndUpdate({ _id: book._id },
{ $set: { quantity : book.quantity } }, { upsert: true },
(err, complete) => {
if(err) console.log('errrrrr');
else {
console.log(complete)
console.log('complete')
res.json(complete)
}
})

How to delete all documents in DocumentDB using NodeJS?

I have one collection user, which has many different properties.
Q.1 I want to run query with specific query and delete all those documents using nodejs, how can I do that?
Q.2 if I want to delete all documents using nodejs, how can I do this?
async.forEach(orders, function(order, callback) {
client.deleteDocument(colle._self,order, function(err, success) {
if (err) {
callback(err);
} else {
callback(null, success);
}
});
}, function(err, result) {
if (err) {
return respondFailed(res, { 'message': err }, 400);
} else {
respondSuccess(res, null, 0, { message: 'All Orders deleted.' });
}
});
I couldn't find a simple example of bulk delete.
Here is a similar solution working with the #azure/cosmos sdk:
const { resources: users } = await container.items
.query({
query: "SELECT * from u"
})
.fetchAll();
users.map(async usr => {
await container.item(usr.id, usr.pk).delete()
})
Thanks for all your concern. finally I found my mistake.
In my code I was passing coll._self collection link instead of docu._self link.
async.forEach(orders, function(order, callback) {
client.deleteDocument(order._self,order, function(err, success) {
if (err) {
callback(err);
} else {
callback(null, success);
}
});
}, function(err, result) {
if (err) {
return respondFailed(res, { 'message': err }, 400);
} else {
respondSuccess(res, null, 0, { message: 'All Orders deleted.' });
}
});

MongoDB $where is not working properly

I have looked around and tried all kinds of docs to be able to get the $where clause in MongoDB to work for me, but it just won't work.
Here is my object:
var UserProfiles = [{
userProfileID: "3f8c553c-3633-4fe9-a007-4346a783450c",
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunter8....com',
token: '',
platform: '',
password: 'admin',
companyProfileID: "86660a5b-7f61-4238-889d-1cc3087947b9",
authentication: ''
}....
there are several "profiles" inserted into the UserProfiles Object. That is just the first one.
Here is me inserting into the collection:
MongoClient.connect(url, function(err, db) {
if (err) {
console.log(err);
} else {
console.log("We are connected");
}
var collection = db.collection('UserProfile');
for (var i = 0; i < UserProfiles.length; i++) {
collection.insert(UserProfiles[i], function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
}
db.close();
});
Now I am trying to search my collection for a passed in email AND companyProfileID. If they both match then return that profile. I thought the $where clause would be best but I can't get it to work.
Here is me trying to find():
function getUserProfile(passInEmail, companyID, callback) {
MongoClient.connect(url, function(err, db) {
if (err) {
console.log(err);
} else {
console.log("We are connected");
}
var collection = db.collection('UserProfile');
collection.find({$where: "this.email == passInEmail"}, function(err, result) {
if (err) {
console.log(err);
callback(err);
} else if (result.length) {
console.log(result);
callback(result);
} else {
callback(err);
console.log("No document found");
}
});
db.close();
});
}
I am trying to search the collection and if the object email matches the passed in email and the object companyProfileID matches the passed in companyID then success.
The $where clause in your case in not the best thing.
You should do simply:
//userIdParam ad emailParam are two variables
collection.find({userProfileID: userIdParam, email: emailParam})
.toArray(function(err, result) {
if (err) {
console.log(err);
callback(err);
} else if (result.length) {
console.log(result);
callback(result);
} else {
callback(err);
console.log("No document found");
}
});
Take a look of the doc here

Callback/Promises implementation for a boolean check

Currently I have the following callback system:
var saveTask = function(err, result) {
if (err) return callback(err, result);
var newid = mongoose.Types.ObjectId();
var task = new Task({
_id: newid,
taskname: req.body.name,
teamid: req.body.team,
content: req.body.content,
creator: req.user.userId
});
task.save(function (err) {
if (!err) {
log.info("New task created with id: %s", task._id);
return callback(null, task);
} else {
if(err.name === 'ValidationError') {
return callback('400', 'Validation error');
} else {
return callback('500', 'Server error');
}
log.error('Internal error(%d): %s', res.statusCode, err.message);
}
});
};
if (req.body.team) {
valTeam.isMember(req.body.team, req.user._id, function (err, done) {
if (err) {
saveTask('403', 'Not the owner or member of this team');
} else {
saveTask(null, true);
}
});
} else {
saveTask(null, true);
}
valTeam.isMember
exports.isMember = function(teamid, userid, callback) {
Team.find({'_id':teamid, $or:[{'creator': userid }, {'userlist': { $in : [userid]}}]}, function(err, result) {
if (err) return err;
console.log(result);
if (!result.length)
return callback('404', false);
else
return callback(null, true);
});
}
In short, if team is sent by POST, I'm checking if the user is member of that ID in valTeam.isMember. Am I using the correct syntax and best method to call back my saveTask function to save the task if the user is part of the team?
This code currently works, but I feel like there should be an easier way to do it? How could I use a promise to achieve the same thing?
Thanks in advance.
It's curious the fact that you create objects instead Schemas. However "every head is a different world", this is my way:
task.save(function(error, data){
if (error) {
trow error;
} else {
//Make whatever you want here with data
});

Categories