How do you replace the entire items of a mongodb collection and with a Javascript array? - mongodb

I have an original mongodb collection that looks like this that populates in my app:
{"_id": "57d5af7de3a3885aa7381380", "name": ["Barry Allen", "Bruce Banner",
"Clint Barton",
"Arthur Curry",
null]}
How can I replace my mongodb collection with a javascript array that looks like this:
var newRoster =
["Peter Parker", "Susan Storm", "Victor Stone",
null, "Jennifer Walters];
I tried doing this in my js file.
$.ajax({
method: 'POST',
url: '/students',
data: {
updatedRoster: newRoster,
},
success: function(response){
console.log(response);
if (response == 'success'){
alert('Successfully updated');
} else {
alert('Error');
}
}
});
I tried going this in my node file.
app.post('/students', function(req, res){
db.students.update({"name": req.body.updatedRoster},
function(err, docs){
if (err) throw err
res.send('success');
});
});

The first object in the update function is a selector. It tells mongoDB which document you want to update. The second object is used to tell mongoDB how you want the found document(s) to be changed.
So, try this:
// client
$.ajax({
method: 'POST',
url: '/students',
data: {
_id: id,
updatedRoster: newRoster
},
success: function(response){
console.log(response);
if (response == 'success'){
alert('Successfully updated');
} else {
alert('Error');
}
}
});
// server
app.post('/students', function(req, res){
db.students.update(
{ "_id": req.body._id },
{ $set: { "name": req.body.updatedRoster } },
function(err, docs) {
if (err) throw err
res.send('success');
}
);
});

Related

How can I return an updated document from mongodb/mongoose

I'm trying to return the updated document from mongodb but instead I'm console logging Null, any ideas why this might be happening? It's updating the document in the database, but it doesn't seem to want to return the full document to me so I can console log it.
User.findOneAndUpdate({userEmail}, {$set: {resetPasswordToken: token, resetPasswordExpires: now}}, function (err, res){
console.log(res);
})
Use the {new: true} option.
User.findOneAndUpdate({
email: userEmail
}, {
$set: {
resetPasswordToken: token,
resetPasswordExpires: now
}
}, {
new: true
}, function(err, res) {
console.log(res);
})

mongoose When I Use update it updates Nothing with status 200(success)

I use update Query for push some data in array in Mongodb and I use mongoose in nodeJs.Pplease anyone can help out from this.
Model Schema :
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt')
var schema = new Schema({
email: { type: String, require: true },
username: { type: String, require: true },
password: { type: String, require: true },
creation_dt: { type: String, require: true },
tasks : []
});
module.exports = mongoose.model('User',schema)
So I use this schema and I want to push data in tasks array and here is my route code for pushing data.
Route For Update Data in Tasks:
router.post("/newTask", isValidUser, (req, res) => {
addToDataBase(req, res);
});
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime,
};
var usr = new User(req.user);
usr.update({ email: req.user.email }, { $push: { tasks: dataa } });
console.log(req.user.email);
try {
doc = await usr.save();
return res.status(201).json(doc);
} catch (err) {
return res.status(501).json(err);
}
}
Here I create a async function and call that function in route but when I post data using postman it response with status code 200(success) but it updates nothing in my database.
Output screenshot:
as you can see in this image task : [].. it updates nothing in that array but status is success
I don't know why is this happening.
You can achieve this task easier using findOneAndUpdate method.
router.put("/users", isValidUser, async (req, res) => {
var data = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime,
};
try {
const user = await User.findOneAndUpdate(
{ email: req.user.email },
{
$push: {
tasks: data,
},
},
{ new: true }
);
if (!user) {
return res.status(404).send("User with email not found");
}
res.send(user);
} catch (err) {
console.log(err);
res.status(500).send("Something went wrong");
}
});
Also I strongly suggest using raw / JSON data for request body, that's how most ui libraries (reactjs, angular) send data.
To be able to parse json data, you need to add the following line to your main file before using routes.
app.use(express.json());
TEST
Existing user:
{
"tasks": [],
"_id": "5e8b349dc285884b64b6b167",
"email": "test#gmail.com",
"username": "Kirtan",
"password": "123213",
"creation_dt": "2020-04-06T14:21:40",
"__v": 0
}
Request body:
{
"pName": "pName 1",
"pTitle": "pTitle 1",
"pStartTime": "pStartTime 1",
"pEndTime": "pEndTime 1",
"pSessionTime": "pSessionTime 1"
}
Response:
{
"tasks": [
{
"pName": "pName 1",
"pTitle": "pTitle 1",
"pStartTime": "pStartTime 1",
"pEndTime": "pEndTime 1",
"pSessionTime": "pSessionTime 1"
}
],
"_id": "5e8b349dc285884b64b6b167",
"email": "test#gmail.com",
"username": "Kirtan",
"password": "123213",
"creation_dt": "2020-04-06T14:21:40",
"__v": 0
}
Also as a side note, you had better to create unique indexes on username and email fields. This can be done applying unique: true option in the schema, but better to create these unique indexes at mongodb shell like this:
db.users.createIndex( { "email": 1 }, { unique: true } );
db.users.createIndex( { "username": 1 }, { unique: true } );
It's been awhile since I've done mongoose, but I'm pretty sure <model>.update() also actively updates the record in Mongo.
You use .update() when you want to update an existing record in Mongo, but you are instantiating a new User model (i.e. creating a new user)
try the following code instead for a NEW USER:
router.post('/newTask', isValidUser, (req, res) => {
addToDataBase(req,res)
})
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime
}
// email field is already in `req.user`
var usr = new User({ ...req.user, tasks: [dataa] });
console.log(req.user.email);
try {
await usr.save();
return res.status(201).json(doc);
}
catch (err) {
return res.status(501).json(err);
}
}
Now, if you wanted to update an existing record :
router.post('/newTask', isValidUser, (req, res) => {
addToDataBase(req,res)
})
async function addToDataBase(req, res) {
var dataa = {
pName: req.body.pName,
pTitle: req.body.pTitle,
pStartTime: req.body.pStartTime,
pEndTime: req.body.pEndTime,
pSessionTime: req.body.pSessionTime
}
try {
await usr. updateOne({ email : req.user.email}, { $push: { tasks: dataa } });
return res.status(201).json(doc);
}
catch (err) {
return res.status(501).json(err);
}
}
For more info read: https://mongoosejs.com/docs/documents.html

Query mongoDB with mongoose model

Im using the find() method to get all data from a mongoDB like this
exports.getPersona = function (req, res){
Persona.find( function(err, persona) {
if (err)
res.send(err)
res.json(persona); // return all personas in json
}
);
}
Im trying to do a query like this:
exports.getPersona = function (req, res){
Persona.find(
{
faceDetection: {
name: "Borja"
}, function(err, persona) {
if (err)
res.send(err)
res.json(persona);
}
);
}
But i dont find nothing. My mongoose model is:
var faceDetectionSchema = mongoose.Schema({
faceDetection: {
name: String,
surname: String,
}
});
What could be the problem? Thanks a lot.
Edit: the document its a JSON like:
{
"faceDetection": {
"name": "Borja",
"surname": "Good"
}
Looks like your parentheses and braces are out of order. Try this below.
exports.getPersona = function(req, res) {
Persona.find({
'faceDetection.name': "Borja"
}, function(err, persona) {
if (err)
res.send(err)
res.json(persona);
})
}

Updated date field is not updated

I have defined this schema
var docSchema = mongoose.Schema({
name:{type:String,required:true},
}, { timestamps: { createdAt: 'createdAt',updatedAt:'updatedAt' }, collection : 'docs', discriminatorKey : '_type' });
I update the documents using this route
router.post('/:id', auth, function(req,res,next) {
var id = req.params.id;
docA.findByIdAndUpdate(id, req.body, {new: true}, function(err, doc) {
if(err)
res.json(err);
else if(doc==null)
res.status(404).send({
message: "Document not found"
});
else
res.json(doc);
});
});
I noticed updatedAt is not updated when I save some edits to the documents.
Besides this problem, thinking about it, it could be helpful to keep this data in form of array of updated date like:
updatedAt : [
"2016-10-25T12:52:44.967Z",
"2016-11-10T12:52:44.967Z",
"2016-12-01T12:52:44.967Z"
]
SOLUTION(?):According to #chridam suggestions, my current workaround to keep an array of update Dates is:
docSchema.pre(`findOneAndUpdate`, function(next) {
if(!this._update.updateHistory) {
console.log("findOneAndUpdate hook: updateHistory not present")
this._update.updateHistory=[];
}
this._update.updateHistory.push(new Date);
return next();
});
docSchema.pre('save', function (next) {
if(!this.updateHistory) {
console.log("Save hook: updateHistory not present")
this.updateHistory=[];
}
this.updateHistory.push(new Date);
next();
});
This is a known issue, please refer to the original thread on the plugin here, where dunnkers commented:
It's actually impossible to hook middleware onto update,
findByIdAndUpdate, findOneAndUpdate, findOneAndRemove and
findByIdAndRemove in Mongoose at the moment.
This means that no plugin is actually run when using any of these
functions.
Check out the notes section in the Mongoose documentation for
middleware. Issue Automattic/mongoose#964 also describes this.
As a suggested workaround, factoring in your schema changes:
var docSchema = mongoose.Schema({
"name": { "type": String, "required": true },
"updateHistory": [Date]
}, {
"timestamps": {
"createdAt": 'createdAt',
"updatedAt": 'updatedAt'
},
"collection" : 'docs',
"discriminatorKey": '_type'
});
router.post('/:id', auth, function(req,res,next) {
var id = req.params.id;
docA.findByIdAndUpdate(id, req.body, {new: true}, function(err, doc) {
if(err)
res.json(err);
else if(doc==null)
res.status(404).send({
message: "Document not found"
});
else {
doc.updateHistory.push(new Date());
doc.save().then(function(doc){
res.json(doc);
}, function(err) {
// want to handle errors here
})
}
});
});
Another approach would be to attach a hook to the schema:
docSchema.pre("findOneAndUpdate", function() {
this.updatedAt = Date.now();
});

How can I remove an object from an array?

I want to remove an object from an array. Here is the schema I'm working with:
event: {
invitees: {
users : [{
user: {
type: String,
ref: 'User'
},
}],
}
}
The query I'm using is listed below, but it isn't working. Basically, nothing happens when I run this script.
Event.update(
{"_id": req.params.event_id},
{"$pull": {"invitees.users.user": req.params.user_id}},
{safe: true, upsert: true},
function (err, data) {
if (err) {
console.log(err);
}
return res.json({ success: true });
}
);
What am I doing wrong?
The field of the $pull operator identifies the array to pull the elements from that match its query.
So your update should look like this instead:
Event.update(
{"_id": req.params.event_id},
// { $pull: { <array field>: <query> } }
{"$pull": {"invitees.users": {"user": req.params.user_id}}},
{safe: true, upsert: true},
function (err, data) {
if (err) {
console.log(err);
}
return res.json({ success: true });
}
);