ExpressJS - switching between multiple categories in API requests - node.js

I'm currently fetching below API from http://localhost:3009/api/get-products/ .
How do I switch between 10 categories and search using other keywords ?
app.get("/api/get-products",async (req, res) => {
client.execute('affiliate.product.query', {
'app_signature':'mcc',
'category_ids':'111,222,333', <--- query different categories
'keywords':'shirt', <--- query different search keyword
'target_currency':'USD',
'target_language':'EN',
}

You should use query parameters that allow you to specify what keywords or categories you want to receive:
Look at that example:
https://example.com/over/there?name=ferret.
While passing a query like that, you are able to get name from req object.
Here you can find more about the query parameters in Node.js: How to get GET (query string) variables in Express.js on Node.js?

Related

How to query multiple documents from multiple nested subcollections in firebase firestore?

I have 3 subcollections nested under one another under one single main collection.
I want to get all the documents under 'colection3' for each document in 'collection2' for each document in 'collection1'
I want to query something like -
admin.firestore().collection('collection1').doc('FOR ALL DOCS IN COLLECTION 1').collection('collection2').doc('FOR ALL DOCS IN COLLECTION 2').collection('collection3').get()
My question is, can I make such query ? Will following query work ?
collection('collection1/*/collection2/*/collection3')
Is this a valid path? What does "*" indicates?
I tried something like this,
const baseRef = admin.firestore().collection(`collection1/*/collection2/*/collection3`);
const querySnap = baseRef.get()
It returned me a querySnapshot but when I tried to loop through this querySnapShot, it didn't print anything
querySnap.forEach(doc => console.log(doc.id))
output was nothing.
I was expecting that doc Ids should get printed in the console.
This can be achieved with collection group level queries.
The collectionGroup option returns all documents within the collection group you have created.
const allDocsQuery = admin.firestore()
.collectionGroup('collection1');
await allDocsQuery.get().then((snap) => {
// do operations with your data
});
Note that you will need to create a collection group in order to use this - it will throw an error with a link to the creation page, but you can also do it by following the documentation in the link above.

Is there a way to filter through data based on if a search query is present in the string in mongodb?

I have data that looks like this in my mongoDB atlas database:
object: {
keywords: ['Homelessness', 'Food', 'Poverty']
}
I'm creating a filtering component for my MERN stack website and wanted to add a search feature for keywords like these that are present in each object in the database. If a search query was Homelessness for example, then the object above would show up since it has Homelessness as one of its keywords. But say for example I enter Homeless as a search query, the ones with Homelessness won't pop up because Homeless =/= Homelessness. Is there a way to somehow find if the search query is within a string inside an array which is all inside a json object?
Here is what I tried so far which gets the result I described in the situation above:
const getFilteredProjects = async (req, res) => {
// Initializing request object that will be sent to retrieve DB information
var request = {}
if (req.query.keywords !== '') {
request["keywords"] = req.query.keywords
}
console.log(request)
const projects = await Project.find(request).sort({ assignment_type: 1 })
res.status(200).json(projects)
}
How can I somehow access each string inside the keywords array and see if the search query is present in it? Is that possible with mongodb or would I have to somehow do it through javascript? If that's the case I'm not sure how I could do that, I would appreciate it if I could get some help.

Not able to search a query using find methon properly in mongoDB

Here I am trying to find all those documents having searched string in cui_str of either population_mesh or intervention_mesh or outcome_mesh
I am using $or property to find from three different fields. I am using below query from nodejs to fetch from mongodb.
exports.searchQuery= (req,res) => {
const picoSearch = req.query.text
Pico_tests.find({$or : [{"population_mesh.cui_str" : {$regex: `${picoSearch}`,$options: '$i'}},{"intervention_mesh.cui_str" : {$regex: `${picoSearch}`,$options: '$i'}},
{"outcomes_mesh.cui_str" : {$regex: `${picoSearch}`,$options: '$i'}}]}).then((data)=>{
res.send(data)
})
// Here i am searching in three different fields but is fetching all the documents irrespective of any string searched.
}
But in the result I am getting all the documents rather than specific documents containing that string. I have check a lot of time in database that given string(i.e Patient) searched but I got all the documents rather than specific one.
It was very silly mistake of spell which make all docs get fetched.

dynamically set the lookup key in a mongo findOne

My REST API allow the user to decide how they want to lookup an item.
They have two options, a default and a second option.
The URL would look like:
/item/{id}
Optionally the user can pass a 'query' on the request i.e
/item/{id}?key=sku
I'm using Node.js, restify and mongoJS. I'm trying to dynamiclly set the "key" in the query as follows:
// default lookup is always id
var queryKey = '_id';
// if the optional 'via' query is set to sku change the lookup key
if(viaVal === 'sku' ){
queryKey = 'sku'
}
mongo.idpool.findOne({queryKey: id},
This causes an "illegal access" error when I'm debugging and does not work.
What is the cause of this?
What is the proper way to achieve this?
You need to use the computed property name syntax when using a variable as a property name by surrounding it in square brackets:
mongo.idpool.findOne({[queryKey]: id}, ...

mongoose: how to get string representation of query

I implementing module who automatically generate mongoose query by requested params, so for simplified test process I need to be able to get text representation of final query. How could I do that?
Like we have something like this:
var q = AppModel.find({id:777}).sort({date:-1})
I need to get something like this
"db.appmodels.where({id:777}).sort({date: -1})"
You can set debug for mongoose, which would by default send the queries to console, to use the following:
mongoose.set('debug', function (collectionName, method, query, doc) {
// Here query is what you are looking for.
// so whatever you want to do with the query
// would be done in here
})
Given a query object q you can rebuild the query using its fields, namely q._conditions and q._update. This is undocumented though and could easily break between versions of Mongoose (tested on Mongoose 4.0.4).

Resources