How to update metadata in mongoDB(Nodejs).
gfs.collection('uploads').updateOne({ filename:image}, {$set:
{metadata.likes:1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
As your code will work if you add metadata.likes in brackets like
gfs.collection('uploads').updateOne({ filename:image}, {$set:
{'metadata.likes':1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
In this case "metadata.likes" would be equal to 1. Every time it will update it and set it to 1. If you want to increment "metadata.likes" by 1, so try this
gfs.collection('uploads').updateOne({ filename:image}, {$inc:
{'metadata.likes':1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
Related
I've got the next data in the MongoDB:
{
...
name: Alex,
createdAt: {"$date":"2021-03-09T20:01:57.488Z"},
...
}
And I try to implement the range date query like this:
MongoClient.connect(url, (err, db) => {
if (err) {
return reject(err);
}
const dbo = db.db('dbname');
dbo.collection('users').find({
createdAt: {
$gte: new Date(dateFrom).toISOString(),
$lt: new Date(dateTo).toISOString()
}
}).toArray(
(err, res) => {
if (err) { return reject(err); }
db.close();
return resolve(res);
});
});
And always have zero-result without errors..
How coud I do the right query?
Im trying to query if this field exists on any records in the database but it always comes up null, is there any way to find this val
users.find({_user: { '*': {"$exists": true } }},).toArray(function(err, result) {
if (err) throw err;
console.log(result);
);
Try this:
users.find({_user: {"$exists": true }}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
);
Here is a working snippet: https://mongoplayground.net/p/LzEoeJ2a86L
I am trying to learn mongoose, I come from a SQL background and I am having difficulties figuring out how this works.
So I am essentially trying to run this SQL code in Mongoose
$sql = "SELECT SUM(comdataPurchase)
FROM fuel566243
WHERE fuelDate BETWEEN '$startDate' AND '$endDate';";
But I cannot get it to work properly for me in mongoose. Here is what I have so far.
FuelReceipt.aggregate([{$match:{date:{$gte: fuelStart, $lt: fuelEnd}}},
{$group:{truckNumber: truckNumber, dieselGallons:{'$sum': '$count'}}}]).exec(function(err, data){
if(err){
console.log('Error Fetching Model');
console.log(err);
}
console.log(data);
});
Thank you in advance for your help <3
edit:-------------
This works:
FuelReceipt.aggregate([
{$group:{_id: '$truckNumber',
dieselGallons:{$sum: '$dieselGallons'}}}]).exec(function(err, data){
if(err){
console.log('Error Fetching Model');
console.log(err);
}
console.log(JSON.stringify(data, null));
});
This gives empty array:
FuelReceipt.aggregate([
{$match:{date:{$gte: fuelStart, $lte: fuelEnd}}},
{$group:{_id: '$truckNumber',
dieselGallons:{$sum: '$dieselGallons'}}}]).exec(function(err, data){
if(err){
console.log('Error Fetching Model');
console.log(err);
}
console.log(JSON.stringify(data, null));
});
edit 2:----------------
I figured it out, had to change the data type for the start and end dates.
FuelReceipt.aggregate([
{$match:{date:{$gte: new Date(fuelStart), $lte: new Date(fuelEnd)}}},
{$group:{_id: '$truckNumber',
fuelCost:{$sum: '$fuelCost'}}}]).exec(function(err, data){
if(err){
console.log('Error Fetching Model');
console.log(err);
}
res.send(data);
console.log(JSON.stringify(data, null));
});
The "between" part is implemented correctly however $group requires _id value. You can set it to null if you want to get single aggregate for entire set of documents:
{$group: { _id: null, dieselGallons: {'$sum': '$comdataPurchase'}}}
I have some trouble making queries inside a callback running in Meteor Mongo. Anyone is seeing what I am doing wrong here? Thanks!
var houseInserted = Houses.insert({
building_number: address.building_number,
},function(error, results){
if(error) throw error;
console.log(results); // runs
Meteor.users.update( { _id: this.userId }, // Does not execute
{ $set: { 'profile.housed': true , 'profile.address' : results }
}, function(err, doc){
if (err) throw err;
console.log(results); // runs
console.log(doc);
});
console.log(results); // runs
}
);
Using monk:
var doc =
yield new Promise(function (resolve, reject) {
tokens.findAndModify({
query: {
token: myTokenVar
},
remove: true,
new: false
}, function (err, res) {
if (err)
throw err;
resolve(res);
});
});
The following code above removes every field from the given document but however leaves the document with only the _id field left. It does not completely remove the document.
According the findAndModify source code, the opts object must be provided as a separate parameter. Please try it with the following codes
tokens.findAndModify(
{ query: {
token: myTokenVar
}},
{remove: true, 'new': false},
function (err, res) {
if (err)
console.log(err);
else
console.log(res);
});