I have the following Mongoose model:
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
facebook: {
name: String,
email: String,
customerId: String
}
});
var User = mongoose.model('User', userSchema);
When I update a part of this document using findByIdAndUpdate
User.findByIdAndUpdate(id, {
$set: {
facebook: {
name: name
}
}
});
name gets updated, while email and customerId get removed (unset?).
I didn't find this documented.
Is there a way to update only specific document properties with findByIdAndUpdate?
FindByIdAndUpdate is actually Issues a mongodb findAndModify update command by a documents id.
The point is you are setting an object to overwrite the old object. if you want to update a field you need to modify your update object.
User.findByIdAndUpdate(id, {
$set: {
'facebook.name':name
}
});
This will only update the name field keeping rest of the field of the old object.
Related
How can I do to pass the id of a document from outside and not that mongoose generates it?
I need the objects to be stored with the id I tell them, but mongoose overwrites it and saves it with the one he wants. I tried several ways now and nothing happens.
It is an entity that I am distributing through events in several databases, so I need it to be stored with the id I want to maintain the integrity of the data.
Now I have this and it says "document must have an _id before saving", but the id I have already put it, does not recognize it.
The scheme is like this:
const schema = new Schema({
_id: { type: String },
name : { type: String },
});
I also tried with this, and the error is the same:
const schema = new Schema({
_id: { type : String },
name : { type: String },
},
{
_id: false
});
I am passing the object like this:
Item.create({ _id: 'my uuid here', name: 'something' });
but when it is saved it remains with the id generated by mongoose replacing mine, that is, it changes it to me with a _id: '5twt563e3j5i34knrwnt43w'
Your syntax should work, but sometimes mongoose acts weird.
You can try this syntax (works on my project) :
const item = new Item({ name: 'something' });
item._id = 'my uuid here';
await item.save();
Instead of using a random uuid, you need to use a mongoDB objectID. Mongoose can also create that,
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
Store this id in the collection,
Item.create({ _id: id, name: 'something' });
my mongodb structure
//First user
_id:ObjectId("12345")
name:"prudhvi"
authors:Array
0:Object
authorId:"77777"
authortitle:"medicine"
1:Object
authorId:"66666"
authortitle:"Hospital"
//second user
_id:ObjectId("67890")
name:"venkat"
authors:Array
0:Object
authorId:"55555"
authortitle:"Doctor"
1:Object
authorId:"44444"
authortitle:"Nurse"
Can someone please help here i have two users, On that i need to get only the latest object of authors array. Here my latest Object is 1:Object, If in case one more is added, I need to get 2:Object of data of all users.
I tried like this but i am getting all objects of authors array, But i need to get latest object
userRouter.post('/getAuthors', function (req, res) {
Collections.user.find(req.body.user, function (err, result) {
if (err) res.status(500).send("There was a problem finding the user");
if (result.length > 0) {
res.status(200).send(result[0].authors);
}
}).select({ "authors": 1 });
});
Try using this
Collections.user.find().limit(1).sort({$natural:-1})
Take a look at $natural and cursor.sort
In your mongoose schema you can set timestamps. it will automatically set createdAt time stamp when you create a object from that schema and if you edit that particular object it set updatedAt timestamp.
As a example schema,
const mongoose = require('mongoose');
const markSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
mark: { type: Number },
student: { type: String },
},{
timestamps: true
});
module.exports = mongoose.model('Mark', markSchema);
like this you can set timestamps.
I have a schema that is defined like
var UserSchema = mongoose.Schema({
user: {
ipAddress: String,
pollIDs: [{
id: String
}]
}
});
var User = module.exports = mongoose.model('User', UserSchema);
What I want to create is a route that checks the requests ip address, see if it exists in the database, if it doesn't create a new document with the ipAddress property set accordingly and the current req.body.poll_id to be an element in the pollIDs array.
However, if there is a document with that ip address I want the req.body.poll_id to be pushed into the pollIDs array.
I would demonstrate my first attempt, but I know that I've messed up the parameters on the findOneAndUpdate call.
Should be as simple as:
User.findOneAndUpdate(
{'user.ipAddress': req.body.ipAddress},
{$push: {'user.pollIDs': {id: req.body.poll_id}}},
{upsert: true, new: true},
(err, doc) => {...});
The upsert will take the query object and apply the update operation to it in the case where it needs to insert a new document.
I have a user model, and a log model. The log model is a subdocument of user model. So in my user model I have:
var mongoose = require('mongoose');
var Log = require('../models/log');
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true
},
logsHeld: [
Log
]
});
Then in my 'Log' model I have:
var mongoose = require('mongoose');
var logSchema = new mongoose.Schema({
logComment: {
type: String,
},
});
module.exports = mongoose.model('Log', logSchema);
So upon creation of a 'user', the 'logsHeld' always begins empty. I want to know how to add subdocuments to this user model.
I've tried doing this POST method:
router.post('/createNewLog', function(req, res) {
var user = new User ({
logssHeld: [{
logComment: req.body.logComment
}]
});
user.save(function(err) {
if(err) {
req.flash('error', 'Log was not added due to error');
return res.redirect('/home');
} else {
req.flash('success', 'Log was successfully added!');
return res.redirect('/home');
}
});
});
But this doesn't work. It also includes a 'new User' line, which I don't think I need given this would be for an existing user.
You need to use the logSchema instead of the Log model as your subdocument schema in User model. You can access the schema as follows:
var mongoose = require('mongoose');
/* access the Log schema via its Model.schema property */
var LogSchema = require('../models/log').schema; // <-- access the schema with this
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true
},
logsHeld: [LogSchema]
});
Picking up from your comments in another answer where you are facing another issue
WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key
error index: testDB.users.$email_1 dup key:
you are getting this because there's already a document in your users collection that has most probably a null value on the email field. Even though your schema does not explicitly specify an email field, you may have an existing old and unused unique index on users.email.
You can confirm this with
testDB.users.getIndexes()
If that is the case and manually remove the unwanted index with
testDB.users.dropIndex(<index_name_as_specified_above>)
and carry on with the POST to see if that has rectified the error, I bet my $0.02 that there is an old unused unique index in your users collection which is the main issue.
Try using logSchema which references only the subdocument schema, Log refers to the entire contents of ../models/log
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true
},
logsHeld: [
logSchema
]
});
Documentation: http://mongoosejs.com/docs/subdocs.html
Try push to insert item in array in mongoose
var user = new User;
user.logssHeld.push({
logComment: req.body.logComment
});
user.save(function(err, doc) {
//DO whatever you want
});
see the docs here
I just noticed that when i update my schema definition and add a field, for instance "name: String" and then try to use
People.update( { _id: user_id }, { $set: { name: 'something' } } )
mongoose won't update my property.
I keep getting nModified: 0 on the response.
The only way i found to fix it, is to Drop the collection and then the new documents will work perfectly.
Am i missing something? Does mongoose somehow "caches" the schema of a collection on mongodb itself and then needs a "drop" in order to "reload" the properties?
I think findbyidandupdate will do the task for you. Try with this link
Mongoose - findByIdAndUpdate - doesn't work with req.body
Can you please share your People model and also please use callback with update.
see below and works fine..
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var User = mongoose.model('User', { email: String, name: String });
//First added only email and used save to save the document.
//var user = new User({ email: 'john.due#example.com' });
User.update({ _id: '55fbbb268e7307dc0bf9ae92' }, { $set: { name: 'John Due' }}, function(err, result) {
if(err) throw err;
console.log(result)
});
Stop your node app and restart the app. It should work.