Node.js mongoose advance Search get query from user - node.js

I'm trying to set raw query from node to mongodb with mongoose driver.
I got the query from user using javascript
var keys = $('#advance-search').val();
then I Post the keys to the node server and trying to using the query in find() like the following:
var search = req.query.keys || ""
Product.find(keys)...
the problem is the type of keys is String and find() need object
when I tried to convert keys to object by keys = JSON.parse(keys) I got exception because the mongo query not a valid Json format
example: {'product.price' :{ $in :[500,400,300,600]}}
so what is the best way to make a raw query from html to mongo by node js, and how I can filter this query before execution?

Related

How do I write this query without using raw query in sequelize?

I would like to a bulk update in sequelize. Unfortunately it seems like sequelize does not support bulk updates I am using sequelize-typescript if that helps and using postgresql 14
My query in raw SQL looks like this
UPDATE feed_items SET tags = val.tags FROM
(
VALUES ('ddab8ce7-afa3-824f-7b65-edfb53a71764'::uuid,ARRAY[]::VARCHAR(255)[]),
('ece9f2fc-2a09-4a95-16ce-07293b0a14d2'::uuid,ARRAY[]::VARCHAR(255)[])
) AS val(id, tags) WHERE feed_items.id = val.id
I would like to generate this query from a given array of string and array values. The tags is implemented as a string array in my table.
Is there a way to generate the above query without using raw query?
Or an SQL injection safe way of generating the above query?

How can I find object ID of MMT4 by nodeJS and mongoDB?

How can I find object ID of MMT4 by nodeJS and mongoDB ?
Thank you for your help ❤️
Hi I worked on your question and if you are using mongodb and nodejs you can do the following stpes:
using $match and $unwind aggregations to find the object that match with title MMT4:
db.collection.aggregate([{$unwind: "$children"}, {$match: {"children.title": "MMT4"}}])
Notice: I wrote the above syntax in mongodb shell you can write it in JS with mongoose library.
the result is an object with children array that contain just MMT4 object. you can iterate in this single element array using JS to get _id of the MMT4
reference: click here

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?

Mongodb finding a record by native id generated Node.js backend

First time working with MongoDB. This query works for me in cmd line:
db.secrets.update({_id: ObjectId("5f767cd481cea1687b3dbf86")}, {$set: {secret_rating: 5}})
However, updating a record using essentially the same query on my node server is not completing this task when pinged. Am I wrong in trying to query for a record like so in my model? ObjectId obviously isn't native to my server.
db.secrets.update({_id: "5f767cd481cea1687b3dbf86"}, {$set: {secret_rating: 5}})
Assuming you're using the Nodejs Mongodb driver and not some ORM (since it hasn't been specified), two points of concern:
As far as my knowledge serves, if you have a connection object to your desired database in the db variable, you cannot reference collections directly such as you've done with db.secrets; you must instead use the collection method like so:
const secrets = db.collection("secrets");
secrets.find({
/*your query here*/
}).then((results) => {})
So, unless you've assigned db.secrets with db.collection("secrets") you should be getting an error, Cannot read property "update" of undefined. But I'm going to assume you've got the collection object in db.secrets since you did not mention you're getting that error.
You seem to be using a string instead of an ObjectID object. You can import the ObjectID constructor from the nodejs driver like so:
const ObjectID = require('mongodb').ObjectID
Then in your query, you will have to make a new ObjectID to get the correct result:
db.collection("secrets").find({
_id: new ObjectID("5f767cd481cea1687b3dbf86")
}).then((results) => {})
NOTE: The ObjectID constructor will throw an error if the string supplied to it is not a valid, 24-char hex string, so, if you're getting the id string as an input from somewhere (say, as a parameter in an API or as a command line argument), you might want to wrap it in a function that handles that error.

Node.js Mongo DB find query works weirdly

I'm using Node.js MongoDB native driver for this.
When I use condition directly in the find() it Works fine
db.collection('test').find({'age':'25'}).toArray().then((docs)=>{
When I assign condition to a variable and use it in find() it works fine.
var query = {'age':'25'};
db.collection('test').find(query).toArray().then((docs)=>{
But when I assign a value from input by user the query doesn't work as expected instead works as if no condition was specified.
var query = (`{'age':'${age}'}`)
db.collection('test').find(query).toArray().then((docs)=>{
I tried printing the query and I see it has the required condition {'age':'25'}
Any thoughts please help.
Try this:
var query = {'age': `${age}`}

Resources