Say I got this lil chunk of code:
Room.findOneAndUpdate({ Roomid: roomid }, { $push: { UsersMeta: UserMeta}}, { new: false }, function (err, room) {
if (err) console.log(err);
console.log('room output:');
console.log(room);
client.emit('others', room);
})
which is searching for one document in db, updates it, and then sends this room doc in pre-updated state back to client. What I need is to make some changes to responded room, particularly remove those _id, __v, and, possibly, any other custom part of doc.
What I was trying to do:
use toObject.transform while creating schema
var RoomSchema = mongoose.Schema({
Roomid: { type: String, unique: true },
///stuff///
});
RoomSchema.options.toObject.transform = function (doc, ret, options) {
// remove the _id of every document before returning the result
delete ret._id;
}
failed: recieved cannot set property 'transform' of undefined error.
Change mentioned chunk of code to:
Room.find({ Roomid: roomid })
.update({ $push: { UsersMeta: UserMeta} })
.select({ _id: 0 })
.exec(function (err, room) {
if (err) console.log(err);
console.log('room output:');
console.log(room);
client.emit('others', room);
})
Failed: always recieve [] in room output.
Now I stopped at manually setting {_id: false} on Schema declaration, completely getting rid of _id in the first place. As I want use custom random id's for rooms, it seems that I don't need those _id anyway. Yet I'm not sure, that such a treatment will not cause some unpleasant consequences.
And, moreover, problem of possible need to leave some non _id doc properties out of response - is an unsolved mystery for me.
Thank you for attention.
you can do the following and it should work;
RoomSchema.set('toJSON', {
transform: function (doc, ret, options) {
delete ret._id;
delete ret.__v;
}
});
Related
I am saving an updated array of _id's to an array on a document. If I check the document, everything is updating quite nicely, but when I populate right afterward using a new find query, it doesn't work right. populate wasn't working for me, so I decided to handle it manually and I am successful at filling them in, but they're in the wrong order.
export function sort_update(req, res) {
Action.findOneAndUpdate({
_id: req.query.action
}, {
child_actions: req.body.newArray
}, {
useFindAndModify: false, new: true
}, function(err, doc) {
if (err) return res.send(500, {
error: err
});
console.log("child action id strings after update", doc.child_actions); // ["1", "2"] correct!
Action.find({'_id': {$in: doc.child_actions }}, function(err, child_action_docs) {
if (err) return res.send(500, {
error: err
});
console.log("child action objects after fill", child_action_docs); // [{ _id: "2" }, { _id: "1" }] wrong!
doc.child_actions = child_action_docs;
res.send(doc);
});
});
}
Why are they in the wrong order? Does {'_id': {$in: doc.child_actions }} simply not guarantee any order at all? Do I have to re-sort my objects to the doc.child_actions order deliberately or is there a simpler way of telling mongodb to keep the order in the original array?
Pardon the noob question, but can anyone spot what am I doing wrong here? For some reason it looks like it does not go through the save() hook, as nothing is printed to the console...
router.post('/users', (req, res, err) => {
var user = new User(req.body);
user.save().then(function(user) {
console.log('user save()');
})
.catch((err) => {
res.send(err);
});
});
When I'm printing to the console the user var, before the save(), I get to see the right data.
Thanks.
EDIT: Resolved.
I've added the auto flag to _id in my schema, as the error was that no _id was provided/generated at insert() time.
_id: {
type: Schema.Types.ObjectId,
auto: true,
},
I have an issue I've not seen before with the Mongoose findByIdAndUpdate not returning the correct model in the callback.
Here's the code:
var id = args._id;
var updateObj = {updatedDate: Date.now()};
_.extend(updateObj, args);
Model.findByIdAndUpdate(id, updateObj, function(err, model) {
if (err) {
logger.error(modelString +':edit' + modelString +' - ' + err.message);
self.emit('item:failure', 'Failed to edit ' + modelString);
return;
}
self.emit('item:success', model);
});
The original document in the db looks like this:
{
_id: 1234
descriptors: Array[2],
name: 'Test Name 1'
}
The updateObj going in looks like this:
{
_id: 1234
descriptors: Array[2],
name: 'Test Name 2'
}
The model returned from the callback is identical to the original model, not the updatedObj.
If I query the db, it has been updated correctly. It's just not being returned from the database.
This feels like a 'stupid-user' error, but I can't see it. Any ideas greatly appreciated.
In Mongoose 4.0, the default value for the new option of findByIdAndUpdate (and findOneAndUpdate) has changed to false, which means returning the old doc (see #2262 of the release notes). So you need to explicitly set the option to true to get the new version of the doc, after the update is applied:
Model.findByIdAndUpdate(id, updateObj, {new: true}, function(err, model) {...
app.put("/vendor/:id",async (req,res)=>{
res.send(req.params)
await ModelName.findByIdAndUpdate(id, {type: change}, function(err, docs){
if(err){
conslole.log(err)
}else{
console.log(docs)
}
})
})
Example:
app.put("/vendor/:id",async (req,res)=>{
res.send(req.params)
const data = await userModel.findByIdAndUpdate(req.params.id, {isVendor: true},
function(err, docs){
if(err){
conslole.log(err)
}else{
console.log(docs)
}
})
})
I have been following the docs on how to update a trip using Node.js,mongoose,express, as well as a couple of stack overflow questions here and here.
Inside of my Controller I originally had a save, which was working, but I needed to switch it to an update. When I run the code, nothing breaks and the place is properly logged to the console as I would expect, which further indicates that the code is being run, but for some reason it isn't overwriting the item that has the same placeIdentifier.
What am I doing wrong that prevents this from updating?
My controller:
var place = {
identifier: placeIdentifier,
name: placeName,
location: Location,
travelStart: startDate,
travelEnd: endDate,
}
Place.findOneAndUpdate(
{identifier:placeIdentifier},
{ $set: place },
{upsert: true},
function() {
console.log(place)
console.log("place saved...")
}
);
Oh, I got it. Or rather, this person got it: Mongoose: Find, modify, save
User.findOne({username: oldUsername}, function (err, user) {
user.username = newUser.username;
user.password = newUser.password;
user.rights = newUser.rights;
user.save(function (err) {
if(err) {
console.error('ERROR!');
}
});
});
I'm trying to update an existing document by increment a counter and pushing an object to an array.
Here's the schema for User:
var UserSchema = new Schema({
[...]
posts: {
totalWords: { type: Number, min: 0, default: 0 },
_entries: [
{
words: { type: Number, min: 0 },
body: { type: String },
date: Date
}
]
},
});
And here's the update code:
var newPost = {
words: req.body.words,
body: req.body.entry,
date: new Date()
};
User.findOne(req.user._id, function (err, user) {
var previous = user.posts.totalWords;
user.posts.totalWords = previous + newPost.words;
user.posts._entries.push(newPost);
user.save(function (err) {
if (err) return res.send(400, err);
return res.json(newPost);
});
});
I get the following error:
[TypeError: Object.keys called on non-object]
Any ideas on how to solve this?
Answering my own question
I was able to solve the problem by changing:
User.findOne(req.user._id, function (err, user) { [...] });
Into this:
User.findById(req.user._id, function (err, user) { [...] });
I think, if you would like to use findOne you need follow syntax:
User.findOne({'_id': req.user._id}, {}, function (err, user) {
...
});
Not sure about findById() vs. findOne(), but I've had problems with Mongoose objects returning Object.keys called on non-object while saving or updating with mal-formed data or data corresponding to an older schema. While initializing the document, Mongoose was expecting a subdocument of some kind, but the data in the database didn't match that.
For me it usually happens when I change schemas from a simple object (String, Number, etc.) to a more complex subdocument of some kind. The schema types are mixed up and the object won't load, not even to fix the problem. I had to go into my database using the native driver, search for mal-formed documents using the $type operator, and update them individually.