How to update a field using its previous value in MongoDB/Mongoose [duplicate] - mongodb

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 5 years ago.
I know I can do an update using $set:
Contact.update({
_id: request.id
}, {
$set: { name: newNameValue }
}, {
upsert: false
}, function(err) { ... });
But in this case, instead of passing newNameValue, I'd like to use the previous name value to compute the new one. Let's say I want to capitalize the old name, something like:
Contact.update({
_id: request.id
}, {
$set: { name: $old.name.toUpperCase() }
}, {
upsert: false
}, function(err) { ... });

I think this has already been answered here: How to add new data to current string in MongoDB?, so please, check that for a more detailed answer, but anyway, in short, you can't do that with a single query.
The way to do it using Mongoose, as shown in this official Mongoose example:
Contact.findById(request.id, (err, contract) => {
if (err) return handleError(err);
contract.name = contract.name.toUpperCase();
contract.save((err, contractContract) => {
if (err) return handleError(err);
...
});
});

as far as I know it's not possible. You would need to find and then update
Contact
.find({
_id: request.id
})
.exec(function(err, data) {
if (err) {
return ...;
}
Contact.findByIdAndUpdate(request.id, {
$set: {
name: data.name.toUpperCase()
}
}, {
new: true
}, function(err, doc) {
if (err) return ...;
console.log(doc)
});
}

Related

How to return a specific field in mongodb?

Here is my code, it searches the word 'test' through all documents in 'subs' collection and return them.
The thing is I just need two specific fields (id and name).
app.get('/', (req, res) => {
db.collection('subs')
.find({
$text: { $search: 'test' },
})
.toArray((err, result) => {
if (err) {
throw new err();
}
res.json({
length: result.length,
body: { result },
});
});
});
So you can use a projection:
db.collection('subs').find({$text: { $search: 'test' }}, {name: 1 } ).
Read more about it here: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-_id-field-only
you can set the fields you need in additional argument to the find method :
db.collection('subs').find({
$text: { $search: 'test' }
},
{
name: 1,
otherColumn: 1
}); // select only the "name" & the "otherColumn" column
The _id column is always returned by default, but you could disable it by adding _id: 0.
Hope this solve your question.
Finally I found the answer! :
.find(
{
name: { $in: ['Prison Break', 'Dexter'] },
$text: { $search: 'kill' },
},
{
projection: { name: 1 },
}
)

Mongoose findOneAndUpdate with $addToSet pushes duplicate

I have a schema such as
listSchema = new Schema({
...,
arts: [
{
...,
art: { type: Schema.Types.ObjectId, ref: 'Art', required: true },
note: Number
}
]
})
My goal is to find this document, push an object but without duplicate
The object look like
var art = { art: req.body.art, note: req.body.note }
The code I tried to use is
List.findOneAndUpdate({ _id: listId, user: req.myUser._id },
{ $addToSet: { arts: art} },
(err, list) => {
if (err) {
console.error(err);
return res.status(400).send()
} else {
if (list) {
console.log(list)
return res.status(200).json(list)
} else {
return res.status(404).send()
}
}
})
And yet there are multiple entries with the same Art id in my Arts array.
Also, the documentation isn't clear at all on which method to use to update something. Is this the correct way ? Or should I retrieve and then modify my object and .save() it ?
Found a recent link that came from this
List.findOneAndUpdate({ _id: listId, user: req.user._id, 'arts.art': artId }, { $set: { 'arts.$[elem]': artEntry } }, { arrayFilters: [{ 'elem.art': mongoose.Types.ObjectId(artId) }] })
artworkEntry being my modifications/push.
But the more I'm using Mongoose, the more it feels they want you to use .save() and modify the entries yourself using direct modification.
This might cause some concurrency but they introduced recently a, option to use on the schema { optimisticConcurrency: true } which might solve this problem.

Mongo's aggregation help - why hardcoding works and not req.body? [duplicate]

This question already has answers here:
req.params.number is string in expressjs?
(1 answer)
Confused about how to get req.body
(1 answer)
Closed 3 years ago.
When I set customer_id = 10001, it returns the response I want.
Using the same number (eg 10001), if I comment/remove the above code and set customer_id = req.body.customerNo, it returns '[]'.
Any idea why the first one works and not the second?
using MongoDB v4.0.5
router.get('/customer/invoice-balance/', function (req, res) {
var customer_id = 10001; // ************ works
var customer_id = req.body.customerNo; // ******** doesn't work
console.log(customer_id) // output ---> 10001
var balanceTotal = function (customer_id, callback) {
Transactions.aggregate([{
$match: {
customerNo: customer_id,
status: "Completed"
}
},
{
$group: {
_id: {
date: {
$dateToString: {
format: "%m-%d-%Y",
date: "$shippedDate"
}
},
customer: "$customerNo",
total: {
$sum: "$extAmount"
}
}
}
},
{
$sort: {
"_id.date": 1
}
}
],
function (err, results) {
console.log("this is the result: ", results);
callback(err, results);
});
};
balanceTotal(customer_id, function (err, results) {
if (err) {
res.send(err);
}
res.json(results);
});
});
updated fix:
router.get('/customer/invoice-balance/:customerNo', function (req, res) {
var customer_id = parseInt(req.params.customerNo);

Mongoose returning NULL on findOneAndUpdate()

To learn the MEAN stack (using Mongoose), I'm creating a StackOverflow type application. I have Questions that are stored in Mongo(v3.0.7) and they have Answer sub-documents.
I am trying to increment the Vote of an Answer, but when the question is returned it is null. I'm pretty sure there's something wrong with the query, specifically where I'm trying to get the answer with the ID I need to modify.
Question Schema:
var questionsSchema = new mongoose.Schema({
answers: [ answerSchema ],
});
Answer Schema:
var answerSchema = new mongoose.Schema({
votes: { type: Number, default: 0 },
});
Querying for _id returns null:
Question.findOneAndUpdate(
{_id: req.params.questionId, 'answers._id': req.params.answerId },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
//question is returned as NULL
res.json(question);
});
Querying for 0 votes works:
Question.findOneAndUpdate(
{_id: req.params.questionId, 'answers.votes': 0 },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
//question is returned as NULL
res.json(question);
});
UPDATE:
Query through Mongo the result is returned:
db.questions.find({_id: ObjectId('562e635b9f4d61ec1e0ed953'), 'answers._id': ObjectId('562e63719f4d61ec1e0ed954') })
BUT, through Mongoose, NULL is returned:
Question.find(
{_id: Schema.ObjectId('562e635b9f4d61ec1e0ed953'), 'answers._id': Schema.ObjectId('562e63719f4d61ec1e0ed954') },
Try to use mongoose Types ObjectID
http://mongoosejs.com/docs/api.html#types-objectid-js:
var ObjectId = mongoose.Types.ObjectId;
Question.find({
_id: '562e635b9f4d61ec1e0ed953',
'answers._id': new ObjectId('562e63719f4d61ec1e0ed954')
})
Final answer to the original update question:
Question.findOneAndUpdate(
{_id: req.params.questionId,
'answers._id': new ObjectId(req.params.answerId) },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
res.json(question);
});
You don't need ObjectId at all:
Question.findOne({_id: "562e635b9f4d61ec1e0ed953"}, callback)
Mongoose handles the string for you.
In addition, using find() and querying by _id will result in an array of length 0 or 1. Using findOne() will return the document object.

How to update embedded document in mongoose?

I've looked through the mongoose API, and many questions on SO and on the google group, and still can't figure out updating embedded documents.
I'm trying to update this particular userListings object with the contents of args.
for (var i = 0; i < req.user.userListings.length; i++) {
if (req.user.userListings[i].listingId == req.params.listingId) {
User.update({
_id: req.user._id,
'userListings._id': req.user.userListings[i]._id
}, {
'userListings.isRead': args.isRead,
'userListings.isFavorite': args.isFavorite,
'userListings.isArchived': args.isArchived
}, function(err, user) {
res.send(user);
});
}
}
Here are the schemas:
var userListingSchema = new mongoose.Schema({
listingId: ObjectId,
isRead: {
type: Boolean,
default: true
},
isFavorite: {
type: Boolean,
default: false
},
isArchived: {
type: Boolean,
default: false
}
});
var userSchema = new mongoose.Schema({
userListings: [userListingSchema]
});
This find also doesn't work, which is probably the first issue:
User.find({
'_id': req.user._id,
'userListings._id': req.user.userListings[i]._id
}, function(err, user) {
console.log(err ? err : user);
});
which returns:
{ stack: [Getter/Setter],
arguments: [ 'path', undefined ],
type: 'non_object_property_call',
message: [Getter/Setter] }
That should be the equivalent of this mongo client call:
db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")})
Running:
mongoose v1.8.1
mongoose-auth v0.0.11
node v0.4.10
when you already have the user, you can just do something like this:
var listing = req.user.userListings.id(req.params.listingId);
listing.isRead = args.isRead;
listing.isFavorite = args.isFavorite;
listing.isArchived = args.isArchived;
req.user.save(function (err) {
// ...
});
as found here: http://mongoosejs.com/docs/subdocs.html
Finding a sub-document
Each document has an _id. DocumentArrays have a special id method for looking up a document by its _id.
var doc = parent.children.id(id);
* * warning * *
as #zach pointed out, you have to declare the sub-document's schema before the actual document 's schema to be able to use the id() method.
Is this just a mismatch on variables names?
You have user.userListings[i].listingId in the for loop but user.userListings[i]._id in the find.
Are you looking for listingId or _id?
You have to save the parent object, and markModified the nested document.
That´s the way we do it
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Profile.findById(req.params.id, function (err, profile) {
if (err) { return handleError(res, err); }
if(!profile) { return res.send(404); }
var updated = _.merge(profile, req.body);
updated.markModified('NestedObj');
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, profile);
});
});
};