mongo find inside sails native not working - node.js

I am trying to query directly on mongo using sails native adapter. I am not getting any result inspite of document present in db. Direct Waterline find functions returns documents. However I want to use native find for some other purpose and trying to make it work. Any suggestions? Output of below : null [].
User.native(function(e,collection){
collection.find({phoneNumber:mPhoneNumber}).toArray(function(e,r){console.log(e,r);});

It worked. Seems orm native does not convert number in string format to integer while querying. Converted mPhoneNumber to Integer using parseInt and it worked.
Here is the updated code
User.native(function(e,collection){ collection.find({phoneNumber:parseInt(mPhoneNumber)}).toArray(function(e,r){console.log(e,r);});

Related

MongoDB legacy uuid query with $in for ids

I am trying to query an old mongoDB collection in node.js with the native driver.
It uses Legacy UUID as its _id field.
In my code I am using Binary values such as: "2u0kLwUZuEWQvjqOjgQU4g=="
and I want to query the collection with find() operation.
When trying to test it directly with mongoDB I am able to use the following query:
db.getCollection('myCollection').find({_id: BinData(3, "2u0kLwUZuEWQvjqOjgQU4g==")})
to find my item.
But what I need to do is to find multiple items.
So I need to use something like this:
db.getCollection('myCollection').find({_ids: { $in: [BinData(3, "2u0kLwUZuEWQvjqOjgQU4g=="), BinData(3, "3u0kLwUZuEWQvjqOjgQU4g==")...]}})
but it does not seem to work and always returns zero records.
I am not sure why? and what might be the correct way to query multiple Legacy UUIDs?

Dayjs to JS Date for Mongo

I need to parse a user supplied date in NodeJS. I found answers stating that using built in Date constructor/parse method is not reliable and I should use some library like Moment. I was confused that there are Moment and MomentJS libraries and found that there is a tiny library DaysJS having the same interface. Parsing works fine but I cannot find a way how to get the JS Date object that I need to pass to Mongo. Is there any aother way than extract the unix milliseconds and pass it to Date constructor?
When I pass daysjs instance to Mongo NodeJS driver, it fails:
const day = dayjs('2020-01-02', 'YYYY-MM-DD');
2020-06-07 10:42:33:093 [error]: Request failedThe dollar ($) prefixed field '$L' in 'info.date.$L' is not valid for storage.
This seems to work:
let publishDate = new Date(1000 * dayjs().unix());
Is it the correct way of daysjs with Mongo?
The error you got is because you tried to pass a DayJS object to mongodb, which it doesn't support.
You can pass a javascript Date object to mongodb. In order to convert a DayJS object to plain javascript Date object you can use .toDate() on the DayJS object
dayjs('2020-01-02', 'YYYY-MM-DD').toDate()

Knex + SQL Server whereIn query 8-12s -- raw version returns NO results but if I input the .toQuery() result directly I get results

The database is in Azure cloud and not being used in production currently. There are 80.000 rows and a uprn is a VARCHAR(100);
I'm already using JOI to validate each UPRN as well;
I'm using KNEX with a SQL Server database with the following whereIn query:
knex(LOCATIONS.table).whereIn(LOCATIONS.uprn, req.body.uprns)
but this takes 8-12s to complete and sometimes timesout. if I use .toQuery() on the same thing, SSMS will return the result within 1-2.
If I do a raw query, the resulting .toQuery() or toString() works in SSMS and returns results. But if I try to use the raw directly, it will return 0 results.
I'm looking to either fix what's making whereIn so slow or get the raw query working.
EDIT 1:
After much debugging and trying -- it seems that the bug is due to how knex deals with arrays, so I made a for-of loop to add ? ? ? for each array element and then inputed the array for all params.
This led me to realizing the performance issue is due to SQL server way of parameterising.
I ended up building a raw query string with all of the parameters and validating the input with Joi string/regex config:
Joi.string()
.min(1)
.max(35)
.regex(/^[a-z\d\-_\s]+$/i)
allowing only for alphanumeric, dashes and spaces which should prevent sql injection.
I'm going to look deeper into security issues with this and might make a separate login that can only SELECT data from that table and nothing more to run with these queries.
Needed to just handle it raw and validate separately.

group by in Flask-MongoAlchemy

i stared new project with flask web-framework with mongoDB. i also used database and access data using Flask-MongoAlchemy. i tried work on different query like .all(), .filter_by(), .get() its work nice. but problem is that how to use aggregate funcation in Flask-MongoAlchemy ? for example i want to use group_by.
i tried following but its still not working.
db.User.qroup_by(name)
its gives following error
'BaseQuery' object has no attribute 'group_by'

sort mongodb collection by date in node.js

I’m new to mongodb and trying to figure out sorting with MongoClient in a node.js/express application.
This works in the mongo command line client:
db.mycollection.find().sort({"date":-1}); // displays by date, newest to oldest
I’m trying to achieve the same thing in my application:
db.collection('mycollection').find().sort({"date":-1}); //order remains the same
How can I achieve the same result as the first query? Thank you.
So, first, I'd recommend using Mongoose. Failing that, however, the Node MongoClient puts things like sorting into the find() arguments, like so:
db.collection('mycollection').find({}, {sort:{date:-1}});

Resources