I am building a server-less app in AWS with Lambda and Node.js. I currently have a MongoDB out at mLab. I am attempting to get the "latest" record based on an ISODate string. Using either findOne() or find w/limit 1 it returns the same record everytime (not the latest one).
I have 2 records in my test table that look like:
{ "field1": "myField", "versionTimestamp": "2017-06-13T18:33:06.223Z" }
{ "field1": "myField", "versionTimestamp": "2017-12-13T18:33:06.223Z" }
No matter what I do it always returns the one from the 6th
col.findOne(q, { "sort": ['versionTimestamp', 'desc'] }, function (err, doc) {
db.close();
if (err) {
sendErrorResponse("500", "Unable to query DB", err);
}
else {
if (doc) {
console.log(doc);
callback(null, doc.invoiceDocument);
}
else {
sendErrorResponse("404", "Record Not Found", "No records found that match those parameters.");
}
}
});
Or with limit 1
col.find(q, { "limit": 1, "sort": ['versionTimestamp', 'desc'] }).toArray(function (err, docs) {
db.close();
if (err) {
sendErrorResponse("500", "Unable to query DB", err);
}
else {
if (docs) {
console.log(docs[0]);
callback(null, docs[0].invoiceDocument);
}
else {
sendErrorResponse("404", "Record Not Found", "No records found that match those parameters.");
}
}
});
Asya found it! It was a malformed array in sort option:
sort takes an array of sort preferences, default being 'asc'. I'm
guessing you want another set of array brackets: [ [ 'field', 'desc']
] – Asya Kamsky yesterday
Related
Node API, where we have Mongo collection of profiles, and every profile have subscription_plan which represent rest of the days they have paid for using app.
Now, i works on part of the backend which should decrease subscription_plan of all profiles by 1.
The problem is, subscription_plan is declared as String, so I can't just decrease it by 1
I tried with this after getting some tips here:
router.put('/reduceSubscription', async (req, res) => {
try {
const updatedProfiles = await Profile.updateMany({ is_active: true }, [
{
$set: {
subscription_plan: {
$toString: {
$subtract: [{ $toInt: '$subscription_plan' }, 1]
}
}
}
}
]).exec();
console.log(updatedProfiles);
} catch (err) {
res.status(500).send({ msg: err.message });
}
});
After testing in postman, i got message:
"msg": "Failed to parse number 'NaN' in $convert with no onError value: Bad digit \"N\" while parsing NaN"
I would appreciate any help or code snippet that will help me solve this problem.
Thanks :)
What you are trying to do (as the error states) is not possible with the current schema simply because your Profile model schema defines subscription_plan as a String and $inc only works on numeric fields.
Either change the schema type for that field or use the aggregate pipeline to update the value by creating a pipeline that has a set of aggregation pipeline operations i.e. 1) get the current value, 2) convert it to a numeric value using $toInt, 3) decrement the value with $subtract and 4) set the new value back to string using $toString:
router.put('/reduceSubscription', async (req, res) => {
try {
await Profile.updateMany(
{ is_active: true },
[
{ '$set': {
'subscription_plan': {
'$toString': {
'$subtract': [
{ '$toInt': '$subscription_plan' },
1
]
}
}
} }
]
).exec();
res.json('Profiles updated');
} catch (err) {
res.status(500).send({ msg: err.message });
}
});
I have an application developed in NodeJS, which works as a REST API and consumes data from MongoDB
In MongoDB I have a collection called 'ftp' with more than 10 million documents with the following structure
{
"_id" : ObjectId("59e7c66911506bd1725cf145"),
"ip" : "72.32.177.76",
"timestamp" : "2017-10-16T02:30:26-04:00",
"data" : {
"banner" : "220-FileZilla Server version 0.9.41 beta\r\n"
}
}
The "data.banner" field is a hased index
From NoodeJs I make an aggregate query that filters a string of text using a regular expression, groups and counts the results.
function getData(timeInit, table, textSearch, res) {
MongoClient.connect(url, function (err, db) {
if (err) throw err;
db.collection(table).aggregate([
{
$match: { 'data.banner': $regex:textSearch}
}, {
$group: {
_id: '$data.banner',
num: { $sum: 1 },
}
},
{
$sort: {
num: -1
}
},{
$limit:5
}
], {
allowDiskUse: true
}
).toArray(function (err, result) {
if (err) throw err;
var timeFinal = new Date();
var data = {
result: result,
timeLapse: (timeFinal - timeInit) / 1000,
numResult: result.length
};
res.send(data);
db.close();
});
});
};
The query with regular expression takes about 8 seconds to return results, an excessive time in my opinion, since the regular expressions are not optimal.
My question is how should I make the filter to search for documents that contain text in an optimal way reducing the response time.
If someone knows how to optimize this type of query I would appreciate it a lot.
I am trying to find a document in a collection. I have been using this query structure on nearly all my collections, but for some reason this isnt working for this collection. Any help?
The only data in this collection right now
[ { _id: 581757143389e565b5cf8124,
companyProfileID: '86660a5b-7f61-4238-889d-1cc3087947b9',
url: 'sentsoftware.com',
appID: 1 } ]
Query Structure:
function getCompany(companyUrl, app, callback) {
MongoClient.connect(url, function(err, db) {
if (err) {
console.log(err);
} else {
console.log("We are connected");
}
var collection = db.collection('Companies');
collection.find({url: companyUrl, appID: app}).toArray(function (err, result) {
if (err) {
console.log(err);
callback(err);
} else if (result.length) {
console.log("found");
callback(result);
} else {
console.log("No document found");
callback(err);
}
});
});
}
I keep getting No document found. But if i were to take out the , appID: app part, it finds the document.
A Mongo query is type specific (although numbers can be cast), you've likely got a string where you are expecting a number.
If you ask for the string "1",
collection.find({appID: "1"})
it will only return documents with a string equal to "1", if you ask for the number 1
collection.find({appID: 1})
it will only return documents with a real number (Double, Long, Int) in.
You can check which types you have with a $type query:
collection.find( { appID : { $type : "string" } } ); // or type 2 for earlier mongo versions
collection.find( { appID : { $type : "double" } } ); // or type 1 for earlier mongo versions
collection.find( { appID : { $type : "int" } } ); // or type 16 for earlier mongo versions
Check out the BSON types and $type in the docs, currently (2016/11/11): https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type
Interestingly it will cast between Doubles, Longs and Ints; so inserting these:
db.collection("temp").insertOne({ "type" : "my double", "num" : new mongo.Double(1.0) });
db.collection("temp").insertOne({ "type" : "my long", "num" : new mongo.Long(1) });
db.collection("temp").insertOne({ "type" : "my int", "num" : 1 });
and searching for:
{ "num" : 1 }
returns the three documents.
In mongodb there is a document like below,
{
"_id": ObjectId("57443657ee5b5ccc30c4e6f8"),
"name": "Kevin",
"email": "kevinwarn#gmail.com",
"password": "$2a$13$iZ0PhuY6VlBF6qC9rUredrG39Fw5pEKkIh.VCLiGsLPZMKYveyzey",
"mobile": "9980896745",
"__v": NumberInt(0),
"ocassionTypes": [
{
"occasiontype": "Anniversary",
"date": "2016-05-30T18:30:00.000Z"
},
{
"occasiontype": "Donation",
"date": "2016-07-24T18:30:00.000Z"
},
{
"occasiontype": "House Warming",
"date": "2016-09-21T18:30:00.000Z"
}
]
}
So I have written a query in Nodejs to search occasiontype element in ocassionTypes array like below,
router.post('/find-registry', function(req, res){
var uEmail = req.body.email;
var uocType = req.body.userOccasion;
var findUserId = function(db, callback) {
var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}}).toArray(function(err, docs1){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs1);
}
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
findUserId(db, function(err,docs1) {
db.close();
if(err) return res.json({result:null})
else
return res.json({result1:docs1});
});
});
});
Using this query I am getting 0th index element, but if I give 1st and 2nd element it always shows only 0th index in the output.
In front end I have given input as shown in the picture below.
file.html
Is there any wrong in my query? please help me to fix this.
your query is right but it will give matched document with full array
just add projection in your query
db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email:1, ocassionTypes: {$elemMatch: {occasiontype:uocType}}})
If you are searching in sub document, mongodb returns all sub-document instead of matched sub-document. You can limit no. of sub-document using following code.
var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email: 1, ocassionTypes: {$elemMatch: {occasiontype: uocType}}}).toArray(function(err, docs1){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs1);
}
});
It is not tested but it should work.
I am trying to get count of data fetched from the database using find() query in mongoose. Now can anyone tell me can i do something like below or do i have to write other function to do that
merchantmodel.find({merchant_id: merchant_id, rating: {'$ne': -1 }, review: {'$ne': "" }}, {'review':1, '_id':0}, {sort: {time_at: -1}}, function(err, docs) {
if (err) {
} else {
if (docs) {
console.log(docs[1].review);
console.log(docs.size()); // Here by writing something is it possible to get count or not
res.json({success: 1, message : "Successfully Fetched the Reviews"});
}
}
});
Convert returned value to array and then use length property
var query = { merchant_id : merchant_id, rating : { '$ne': -1 }, review: { '$ne': "" }};
var projection = { 'review':1, '_id':0 };
var options = { sort: { time_at: -1 } };
merchantmodel.find(query, projection, options).toArray(function(err, docs) {
if (err) {
throw(err);
}
console.log(docs[1].review);
console.log(docs.length);
res.json({success: 1, message : "Successfully Fetched the Reviews"});
});
You can simply do this:
console.log(docs.length);
The docs variable returned by the find() method is an array so docs.length would do the job.
The mongodb native way to do this would be:
db.collection.find( { a: 5, b: 5 } ).count()