NodeJS & MongoDB - Get Objects based on array of ids - node.js

I am having trouble returning getting the Objects in a collection based on an array of ObjectIds.
I have a collection of 10 items. In an iOS app, I am passing up an array of ObjectIds that user has saved as favorites to return just those two in a favorites tab, for example -
["59f85eae4545XXX9b94d53d3", "59f85eae45454XXXb94d76a1"]
For now, I have just hardcoded the ObjectIds at the top of the request using NodeJS just until I get the request working, below is the request -
exports.list_favs = function(req, res) {
Articles.find({
_id: { $in: [
mongoose.Types.ObjectId('59f85aXXXXf1769b94d53ce'),
mongoose.Types.ObjectId('59f85eaeXXXX1769b94d53d3')
]}
}, function(err, articles) {
if (err)
res.send(err);
res.json({
data: {
articles
}
});
});
};
And I have even tried this -
exports.list_favs = function(req, res) {
var ids = new Array( new ObjectId("59f85eaeXXXX1769b94d53d3"), new
ObjectId("59f85aXXXXf1769b94d53ce") );
Articles.find({
_id: { $in: ids}
}, function(err, articles) {
if (err)
res.send(err);
res.json({
data: {
articles
}
});
});
};
Both these give me an error "CastError"
"Cast to ObjectId failed for value \"favourites\" at path \"_id\" for model \"Articles\""
This is how it looks in the database so I am completely baffled as to what I am doing wrong. Image of ObjectId in database
I've followed multiple other answers on StackOverflow with no luck.
Gist to full project code

Do this way :
exports.list_favs = function(req, res) {
Articles.find({
_id: {
$in: [mongoose.Schema.ObjectId('59f85aXXXXf1769b94d53ce'),
mongoose.Schema.ObjectId('59f85eaeXXXX1769b94d53d3')]
}
},
function(err, articles) {
if (err)
res.send(err);
else
res.json({
data: {
articles
}
});
});
};

Related

find and modify obejct in mlab collection by _id with nodejs

i am posting an object to update the current one. Searching by id and replacing it. For some reason i don't get errors but the mlab object is not updated. Am i missing something?
app.post("/api/updateCheck", function (req, res) {
console.log('updating', req.body);
conn.collection("checks").findAndModify({
_id: req.body._id
}, {$set: req.body}, {}, function(err,doc) {
if (err) { console.log(err) }
else { console.log("Updated"); }
});
});
got it. updateOne seems to work. I am posting a check object and retrieving id from it to search the collection and update content accordingly.
// modify content
app.post("api/updateCheck", function(req, res) {
console.log("updating", req.body);
conn.collection("checks").updateOne(
{
_id: new ObjectId(req.body._id)
},
{
$set: {
content: req.body.content
}
},
function(err, doc) {
if (err) {
console.log("error", err);
} else {
console.log('success', doc.modifiedCount);
console.log('??', doc.matchedCounted);
res.status(200).json(res.body);
}
}
);
});

How to delete Element In MongoDB property's array with MongooseJS?

I cannot remove an element inside of an array that is a property of a MongoDB Model.
Please remember this is a NodeJS module mongooseJS and not the real MongoDB so functionalities are not the same..
GOAL: Delete an object from the statusLiked array. | I have also confirmed that the value of status.id is correct.
Model:
Const userSchema = new mongoose.Schema({
myStatus: Array,
statusLiked: Array,
)};
Delete:
1. Deletes the status(works). 2. Delete the status from User.statusLiked(no work).
exports.deleteStatus = (req, res, next) => {
var CurrentPost = req.body.statusid; // sends in the status.id
Status.remove({ _id: CurrentPost }, (err) => {
if (err) { return next(err); }
// vvvv this vvv
User.update( {id: req.user.id}, { $pullAll: {_id: CurrentPost }, function(err) { console.log('error: '+err) } });
req.flash('success', { msg: 'Status deleted.' });
res.redirect('/');
});
};
What happens: The specific status(object) is deleted from the database. But the status still remains in the User.statusLiked array.
What I want to happen: Status to be deleted from the User.statusLiked array and the status to be deleted from the database. Then, reload the page and display a notification.
I got it to work somehow. Working code:
exports.deleteStatus = (req, res, next) => {
var CurrUser = req.body.userid;
var CurrentPost = req.body.post;
Status.remove({ _id: CurrentPost }, (err) => {
if (err) { return next(err); }
console.log('meeee'+CurrentPost+'user: ' +CurrUser);
req.flash('success', { msg: 'Status deleted.' });
res.redirect('/');
});
User.update(
{ _id: new ObjectId(CurrUser)},
{ $pull: { myStatus : { _id : new ObjectId(CurrentPost) } } },
{ safe: true },
function (err, obj) {
console.log(err || obj);
});
};

MongoDB objectID stranger Error

Using a MEAN Stack deployment on Heroku I am able to GET and DELETE Documents with mongoDB's findOne and deleteOne functions. However when I try to PUT a document with the mongoDB updateOne/update function, I receive this error (server side) :
The _id field cannot be changed from {_id: ObjectId('56d4d71191fdc81100974d0b')} to {_id: "56d4d71191fdc81100974d0b"}.
Seems strange because I am using the same method in my server code for updateOne as in findOne (again, findOne works fine):
app.get("/contacts/:id", function(req, res) {
db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
if (err) {
handleError(err.message, "Failed to get contact");
} else {
res.status(200).json(doc);
}
});
});
app.put("/contacts/:id", function(req, res) {
var updateDoc = req.body;
db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
if (err) {
handleError(err.message, "Failed to update contact");
} else {
res.status(204).end();
}
});
});
Any suggestions?
I think you have problem at var updateDoc = req.body
As req.body contains id field and you are searching from object to update by that id, mongodb thinks you are trying to update
id field too which is not allowed.
One solution is to remove id field from your updateDoc object.
e.g.
delete updateDoc._id;
now try again and see if it works.
Your final function should look like
app.put("/contacts/:id", function(req, res) {
var updateDoc = req.body;
delete updateDoc.id;
db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
if (err) {
handleError(err.message, "Failed to update contact");
} else {
res.status(204).end();
}
});
});

Cannot update MongoDB using mongoose

I am trying to update a collection from my database using de node module mongoose. The problem is with $set updates. Here is my code:
// Update a user
app.patch('/user/:user_id', passport.authenticate('bearer', { session: false }),
function (req, res) {
var conditions = { _id: new ObjectId(req.params.user_id)},
updateObj = { $set: req.body }; // {email : "bob#example.com", username: "bob"}
User.update(conditions, updateObj, function callback (err, numAffected, rawResponse) {
if (err) {
res.send(err);
return;
}
// numAffected is the number of updated documents
if (numAffected == 0) {
res.json({ message: 'No user affected'});
return;
}
res.json({ message: 'User updated'});
});
});
If I update an existing key like email, it is updated. But if I want to add a new key, numAffected is always 0 and the rawResponse is undefined.
Any idea of what happens?
Edit
Here is my Schema:
var userSchema = mongoose.Schema({
email : String,
username : String,
password : String
});
In order to set multiple fields in a document, you must set the Multi option in your config, otherwise Mongoose will ignore the continuation, and only update the first doc.
From the docs:
var conditions = { name: 'borne' }
, update = { $inc: { visits: 1 }}
, options = { multi: true };
Model.update(conditions, update, options, callback);
function callback (err, numAffected) {
// numAffected is the number of updated documents
});
Another note here: The numAffected should return as expected, but I can't find any documentation on their site about the raw response, but it should return as expected as well. Do you know of any documentation for this?
I think this is what you really want to do with mongoose to update email and username of a user.
app.patch('/user/:user_id', passport.authenticate('bearer', { session: false }),
function (req, res) {
User.findOneAndUpdate({_id: req.params.user_id},
{
$set: {
username: req.body.username,
email: req.body.email
}
}, function(err, user) {
if (err)
res.send(err);
if (user) {
res.json({message: 'User updated'});
} else {
res.json({message: 'User does not exist'});
}
});
});

MongoDb update a route with two id's?

My models on the front end are not saving. :tid is the team id and :pid is player id.
router.put('/api/players/:tid/:pid', player.update);
update: function(req, res) {
models.Player.update({ _id: req.params.pid }, function(err, player) {
if (err) {
res.json({error: 'Player not found.'});
} else {
console.log(player);
}
})
} // obviously looks a bit illogical, but not sure what to do here 100%
Right now I am just using a static team ID in my backbone collection.
var Backbone = require('backbone'),
PlayerModel = require('../models/player');
module.exports = PlayersCollection = Backbone.Collection.extend({
model: PlayerModel,
url: '/api/players/545d1d72f7895d00008e2f43'
});
So basically that grabs the players with team_id 545d1d72f7895d00008e2f43 but it also wants to post to that route with the players id, which is why my route above requires, :tid/:pid
I am just not sure what my mongoDB update function should look like to properly update the model on the server.
Okay I forgot this was an old gotcha, I had ran into this a while back. I had to add req.body.
update: function(req, res) {
models.Player.update({ _id: req.params.pid }, req.body, function(err, player) {
if (err) {
res.json({error: 'Player not found.'});
} else {
res.json(player);
}
});
}

Resources