how to write findOneAndUpdate query in express.js? - mongodb

i have shown my data , which is stored in database like this
{
"_id": {
"$oid": "5799995943d643600fabd6b7"
},
"Username": "xx",
"Email": "xx#gmail.com",
"Info": "Deactivate",
"Description": "aajdjdjddjdkjddjdjdhdj",
"VerificationCode": "594565",
"VerificationExpires": {
"$date": "2016-10-07T10:20:20.077Z"
}
}
My controller:
if Username, Email, Info are matched I need to update " Info = 'Active' " this is working at the same time i need to delete 'VerificationCode' field and 'VerificationExpires' field how can i achieve this?
exports.updatearticle = function(req, res) {
Article.findOneAndUpdate(
{ "Username":'xx', "Email":'xx#gmail.com', "Info": "Deactivate" },
{ "$set": { "Info": "Active" } },
{ "new": true }
function (err, doc) {
if (err) { // err: any errors that occurred
console.log(err);
} else { // doc: the document before updates are applied if `new: false`
console.log(doc); // , the document returned after updates if `new true`
console.log(doc.Info);
}
}
);
};
above condtion matched and info getting changed but i want to delete VerificationCode,VerificationExpires some one help me out

exports.updatearticle = function(req, res) {
Article.findOne( { "Username":'xx', "Email":'xx#gmail.com', "Info": "Deactivate" }, function(err, result){
if (!err && result) {
result.Info = "Active"; // update ur values goes here
result.VerificationCode = "";
result.VerificationExpires = {};
var article = new Article(result);
article.save(function(err, result2){
if(!err) {
res.send(result2);
} else res.send(err);
})
} else res.send(err);
});
}
home this may help

Related

How to return the a formatted response from a mongo query/projection?

I'm trying to create an API to validate a promocode. I have minimal experience with mongo and the backend in general so I'm a bit confused in what is the best approach to do what I'm trying to accomplish.
I have this PromoCode form in the client. When a user types a promocode I would like for my backend to
verify if the code exists in one of the docs.
if it exists then return that code, the value for that code and the couponId
if the code doesn't exist then return an error.
My db is structured like this. The user will type one of those codes inside the codes: []
{
"_id": {
"$oid": "603f7a3b52e0233dd23bef79"
},
"couponId": "rate50",
"value": 50,
"codes": ["K3D01XJ50", "2PACYFN50", "COKRHEQ50"]
},
{
"_id": {
"$oid": "603f799d52e0233dd23bef78"
},
"couponId": "rate100",
"value": 100,
"codes": ["rdJ2ZMF100", "GKAAYLP100", "B9QZILN100"]
}
My route is structure like this:
router.post('/promoCode', (req, res, next) => {
const { promoCode } = req.body;
console.log('this is the req.body.promoCode on /promoCode', promoCode)
if (!promoCode) {
throw new Error('A promoCode needs to be passed')
}
promoCodesModel
.validatePromoCode(req.body.promoCode)
.then((response) => {
console.log('response inside /promoCode', response)
res.status(200).json({ data: response })
})
.catch((error) => {
res.status(400).json({ result: 'nok', error: error })
})
})
The validatePromoCode function is the following:
const validatePromoCode = async (code) => {
try {
let promoCode = await PromoCodesModel.find(
{"codes": code},
{_id: 0, codes: { $elemMatch: { $eq: code }} })
console.log('This is the promocode', promoCode)
return promoCode
} catch (err) {
throw new Error (err.stack)
}
}
All this seems to sort of work since I get the following response when the code is typed correctly
{
"data": [
{
"codes": [
"COKRHEQ50"
]
}
]
}
when typed incorrectly I get
{
"data": []
}
What I would like to get back is. (How can I accomplish this ?). Thanks
// when typed correctly
{
"data": { value: 50, couponId: "rate50", code: "COKRHEQ50" }
}
// when typed incorrectly
{
"error": "this is not valid code"
}
TL;DR: I would like to return a formatted query with specific values from a mongo query or an error object if that value does not exist on the document object.
Ok just figured it out
To be able to get the this responsed (what I wanted):
{
"data": [
{
"codes": [
"K3D01XJ50"
],
"couponId": "rate50",
"value": 50
}
]
}
I ended up having to do this on validatePromoCode
onst validatePromoCode = async (code) => {
try {
let promoCode = await PromoCodesModel.find(
{ codes: code },
{ _id: 0, codes: { $elemMatch: { $eq: code } }, couponId: 1, value: 1 },
)
return promoCode
} catch (err) {
throw new Error(err.stack)
}
}
But is there a better way on doing this ? Thanks

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

Mongoose: Update nested document

I have following JSON structure,
{
"projectId": "service-request-service",
"projectVersion": [{
"version":"1",
"localConfig": [{
"port": "3003",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "RMQ",
"logLevel": "2",
"version": "1.1",
"created": "03-06-2018 03:11:00 PM",
"active": "N"
},
{
"port": "3004",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "IMQ",
"logLevel": "1",
"version": "1.2",
"created": "07-06-2018 03:11:00 PM",
"active": "Y"
}]
}]
}
Now, I want to update all port and active values of localConfig. I tried using different ways,
using markModified
ProjectConfig.findOne({'projectId' : projectId,
'projectVersion.version' : version})
.exec(function(err,pc){
pc.projectVersion[0].localConfig[0].active = "N";
pc.projectVersion[0].localConfig[0].port = "5555";
pc.markModified('localConfig');
pc.save(function(err,result){
if (err) {
console.log(err);
}
console.log("## SUCCESSFULLY SAVED ");
});
});
Iterating using for loop.
ProjectConfig.findOne({'projectId' : projectId,
'projectVersion.version' : version}).exec(function(err,pc){
for(i = 0; i < pc.projectVersion.length ; i++){
for(j = 0; j < pc.projectVersion[i][envQuery].length ; j++){
pc.projectVersion[i][envQuery][j].active = 'N';
pc.projectVersion[i][envQuery][j].port = '5555';
}
}
pc.save(function (err, result) {
if (err) {
console.log(err);
}
console.log("## SUCCESSFULLY SAVED ");
});
});
Using arrayFilters,
let conditions = {};
let update = {$set: {"projectVersion.$[i].localConfig.$[].port": "5555"}};
let options = {arrayFilters:[{"i.version":"1"}]};
pc.update(conditions,update,options,function(err,result){
if (err) {
console.log(err);
}
console.log("## SUCCESSFULLY SAVED ");
});
But, I am getting below error.
MongooseError: Callback must be a function, got [object Object]
Please provide me the way to update document.
Current version of MongoDB : v3.6.6 & Mongoose : ^5.0.14
Using arrayFilters, I am not applying update method on scheme rather applying on object return by find method.
When I directly apply update method on schema, its working.
let conditions = { "projectId" : "32804-service-request-service" };
let update = { $set: {
"projectVersion.$[i].localConfig.$[j].port" : "5555",
}
};
let options = {arrayFilters:[{"i.version":"1" },{ "j.port" : "3003"}]};
ProjectConfig.update(conditions, update, options, function(err,result){
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(200).json({
message: 'SUCCESS',
obj: result
});
});

Sails js find() finding non-existing users

I'm trying to build a simple server in SailsJS and encountered a problem: I send a POST request to the service in Sails, and I always get a 200 response, even when there's no matching user in the on-disk DB.
My model:
module.exports = {
attributes: {
name: { type: "string" },
lastName: { type: "string" }
}
};
My routes file:
module.exports.routes = {
'post /authTest' : 'UserController.testingAuth'
};
My controller:
module.exports = {
testingAuth : function(req, res) {
var temp = req.param("name");
sails.log(temp);
User.find({ name: 'testing123' }).exec(function(err, user) {
if (err) {
sails.log("inside err block");
return res.serverError(err);
}
sails.log("skipped err block");
return res.json(user);
})
}
};
The way I call the service:
var testUser = { name: 'notMyName', lastName: 'myLastName' };
$http.post("http://localhost:1337/authTest", testUser);
Then on the SailsJS console I see:
debug: notMyName
debug: skipped err block
My local DB has just the following though (localDiskDb.db in .tmp):
{
"data": {
"passport": [],
"user": [
{
"name": "myName",
"lastName": "myLastName",
"createdAt": "2017-11-18T17:26:13.609Z",
"updatedAt": "2017-11-18T17:26:13.609Z",
"id": 1
}
]
},
// some schema stuff, irrelevant here
}
Can someone see anything wrong here? The service receives the posted request object fine, searches for a user that is not in the DB, but finds one anyway?
Check this out:
User.find({ name: 'testing123' }).exec(function(err, user) {
if (err) {
sails.log("inside err block");
return res.serverError(err);
}
sails.log("skipped err block");
return res.json(user);
})
to
User.findOne({ name: 'testing123' }).exec(function(err, user) {
if (err) {
sails.log("inside err block");
return res.serverError(err);
} else if(user){
// user found here
return res.json(user);
} else {
//no user found
return res.json(null);
}
})
If you want to stick with find():
User.find({ name: 'testing123' }).exec(function(err, users) {
if (err) {
sails.log("inside err block");
return res.serverError(err);
} else if(users.length == 0) {
//users is empty array of results
return res.json(users)
} else {
//users is array of N records with criteria 'testing123' in field 'name'
return res.json(users);
}
})

MongoDB - addToSet/push only on upsert

I would like to add a subdocument to an array if it doesn't already exist and then return the newly added subdocument (or at least the array of subdocuments) within one query. Here is an example document structure:
{
"name": "John Smith",
"folders": [
{
"folderName": "Breweries"
"updatedAt": 1450210046338,
"checkins": [
{
"facebookID": "123",
"checkinID": "3480809",
"addedOn": 1450210046338
},
{
"facebookID": "234",
"checkinID": "345254",
"addedOn": 1450210046339
}
],
},
{
"folderName": "Food"
"updatedAt": 1450210160277,
"checkins": [
{
"facebookID": "432",
"checkinID": "123545426",
"addedOn": 1450210160277
}
],
}
],
}
The nested query below checks to see if the new folder's name already exists in the folders array. If it doesn't already exist, it adds the new folder to the folders array:
(using mongoskin here)
mongodb.collection('users').findOne(
{facebookID: facebookID, 'folders.folderName': folderName},
function (err, result) {
if (err) {
deferred.reject(err);
} else if (result !== null) {
deferred.reject(new Error('Folder name already taken'));
} else {
mongodb.collection('users').findOne(
{facebookID: facebookID, 'folders.folderName': folderName},
function (err, result) {
if (err) {
deferred.reject(err);
} else if (result !== null) {
deferred.reject(new Error('Folder name already taken'));
} else {
mongodb.collection('users').findAndModify(
{facebookID: facebookID},
[],
{$addToSet: {folders: newFolder}},
{fields:{'folders': 1}, new: true},
function (err, result) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(result);
}
});
}
});
It seems like you should be able to do this in one query - but I couldn't find a way to achieve $setOnInsert functionality with array operators ($addToSet/$push).