MongoDb : How to make a query to find proper data in nodejs using official driver - node.js

I'm newbie at mongodb.
I Inserted some data looks like below:
#1
{
"_id": ObjectId("566930a12e9952aef88b4568"),
"a_site": {
"name": "amazon",
"url": "amazon.com",
"master": "John"
}
}
#2
{
"_id": ObjectId("5669307b2e9952aef88b4567"),
"a_site": {
"name": "google",
"url": "google.com",
"master": "Paul"
}
}
I want make a query to get "google.com" (#2 a_site > url) using name only.
var cloud_service = db.collection('cloud_service');
cloud_service.find({"a_site":{"name":"google"}});
But this query did not work. Please help me.

You should instead write the query like this:
cloud_service.find({"a_site.name":"google"}});
This is called dot notation.

Related

How to make the mongoose data after populating

for example i have done the populate the data and i don't want the entity at which it's populated for example:
{
"gstin": "27AAATW4187E2UW",
"company_name": "TATA_AUTOCOMP ",
"status": "Hold",
"createdBy": {
"name": "xyz123",
"email": "xyz#yahoo.com",
"mobile_number": 7972512892
}
}
]
right now my data looks like above but i don't want the populated information i want my data just like that anyone help me .
{
"gstin": "27AAATW4187E2UW",
"company_name": "TATA_AUTOCOMP ",
"status": "Hold",
"name": "abcd",
"email": "xyz#gmail.com",
"mobile_number": 1234567890
}
]
in your query when use .populate you can use select and when application run your query just return selected parameters like this :
Model
.findOne({ _id: 'bogus' })
.populate('friendsRequestList', 'name email')
and if you want all result in one level in json you must edit your result with .map .forEach and ....

Facing difficulty using compound query with Elasticsearch JS

I am using the official Elasticsearch package from npm within my node.js application. I was attempting to perform search using compound queries ( bool), But I found that the compound search does not work as expected.
To debug the issue, I tried passing different sets of data for the search query. I found an abnormality wherein the elasticsearch library does not work as expected but the Elasticsearch API does. I'm unable to find this behavior documented anywhere else as well.
I executed 2 sets of code (with the same query) on
1) Node using the official elastic search library
2) Over the Elasticsearch API using Postman
I> Using Elastic Search JS
"index": "bank",
"type": "account",
"body": {
"query": {
"bool": {
"must": [{
"match": {
"address": "avenue"
}
}]
}
}
}
}
II> Using Elastic Search API
"query": {
"bool": {
"must": [{
"match": {
"address": "avenue"
}
}]
}
}
}
The results for the official library come in empty (Empty array), But the results using the elasticsearch API result in the correct set of data.
Another peculiar observation was the below query using elasticsearch JS which works for a single element, but not an array of elements
"index": "bank",
"type": "account",
"body": {
"query": {
"bool": {
"must": {
"match": {
"address": "avenue"
}
}
}
}
}
}
I'm breaking my head over where I'm going wrong, I tried going through docs, stackoverflow and a very little bit of code, And returned empty handed.
Would appreciate any help.
Thanks a lot

How to include fields in api server and remove it before returning to results to client in Graphql

I have a Node.js GraphQL server. From the client, I am trying get all the user entries using a query like this:
{
user {
name
entries {
title
body
}
}
}
In the Node.js GraphQL server, however I want to return user entries that are currently valid based on publishDate and expiryDate in the entries object.
For example:
{
"user": "john",
"entries": [
{
"title": "entry1",
"body": "body1",
"publishDate": "2019-02-12",
"expiryDate": "2019-02-13"
},
{
"title": "entry2",
"body": "body2",
"publishDate": "2019-02-13",
"expiryDate": "2019-03-01"
},
{
"title": "entry3",
"body": "body3",
"publishDate": "2020-01-01",
"expiryDate": "2020-01-31"
}
]
}
should return this
{
"user": "john",
"entries": [
{
"title": "entry2",
"body": "body2",
"publishDate": "2019-02-13",
"expiryDate": "2019-03-01"
}
]
}
The entries is fetched via a delegateToSchema call (https://www.apollographql.com/docs/graphql-tools/schema-delegation.html#delegateToSchema) and I don't have an option to pass publishDate and expiryDate as query parameters. Essentially, I need to get the results and then filter them in memory.
The issue I face is that the original query doesn't have publishDate and expiryDate in it to support this. Is there a way to add these fields to delegateToSchema call and then remove them while sending them back to the client?
You are looking for transformResult
Implementation details are:
At delegateToSchema you need to define transforms array.
At Transform you need to define transformResult function for filtering results.
If you have ability to send arguments to remote GraphQL server, then you should use
transformRequest

ElasticSearch search with querystring and verify another field

I need to translate the following SQL query to ES query:
SELECT *
FROM SKILL
WHERE SKILL.name LIKE 'text' and SKILL.type = 'hard'
I have tried the following using "elasticsearch" library for python3:
query = self.__es.search(index="skills",
body={"from" : skip, "size" : limit,
"query":
{"query_string":
{"query": 'text'}
})
and this worked well. But now, I don't know how to check that the field 'type' is equal to 'hard'.
How can I do that?
Thank you.
You have to use a bool query and in the "must" part put two queries, the full text one and a term one:
{
"query": {
"bool": [{
"match": {
"name": "this is a test"
}
}, {
"term": {
"type": "hard"
}
}]
}
}
Before this you have to store the type property as a keyword field.

How to query by array of objects in Contentful

I have an content type entry in Contentful that has fields like this:
"fields": {
"title": "How It Works",
"slug": "how-it-works",
"countries": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "3S5dbLRGjS2k8QSWqsKK86"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "wHfipcJS6WUSaKae0uOw8"
}
}
],
"content": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "72R0oUMi3uUGMEa80kkSSA"
}
}
]
}
I'd like to run a query that would only return entries if they contain a particular country.
I played around with this query:
https://cdn.contentful.com/spaces/aoeuaoeuao/entries?content_type=contentPage&fields.countries=3S5dbLRGjS2k8QSWqsKK86
However get this error:
The equals operator cannot be used on fields.countries.en-AU because it has type Object.
I'm playing around with postman, but will be using the .NET API.
Is it possible to search for entities, and filter on arrays that contain Objects?
Still learning the API, so I'm guessing it should be pretty straight forward.
Update:
I looked at the request the Contentful Web CMS makes, as this functionality is possible there. They use query params like this:
filters.0.key=fields.countries.sys.id&filters.0.val=3S5dbLRGjS2k8QSWqsKK86
However, this did not work in the delivery API, and might only be an internal query format.
Figured this out. I used the following URL:
https://cdn.contentful.com/spaces/aoeuaoeua/entries?content_type=contentPage&fields.countries.sys.id=wHfipcJS6WUSaKae0uOw8
Note the query parameter fields.countries.sys.id

Resources