Display specific data from a collections in mongodb - node.js

Hi all I am very new in mongodb and I have some problems when I try to do some find in a collections I hope so someone can help me.
Imagine I have the following collection:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
'30/09/2015':'19558',
'29/09/2015':'19558',
'28/09/2015':'19558',
'25/09/2015':'19558',
'24/09/2015':'19558'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
Now I want do some find inside the collections for exmample:
A-I want to bring me all currencies search by date range 05/10/2015 to 01/10/2015 then I need that return something like this:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
B-Also I need to know how do a find that return all the information related with a currency, for example I want searh all releated with the currency 'USD', I need return something similar to:
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
C-And finally I need to know how do a find that return all the information related with a value, for example I want search all the information with value of 0 between 12000, I need return something similar to:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.

Related

How can I obtain a document from a Cosmos DB using a field in an array as a filter?

I have a Cosmos DB with documents that look like the following:
{
"name": {
"productName": "someProductName"
},
"identifiers": [
{
"identifierCode": "1234",
"identifierLabel": "someLabel1"
},
{
"identifierCode": "432",
"identifierLabel": "someLabel2"
}
]
}
I would like to write a sql query to obtain an entire document using "identifierLabel" as a filter when searching for the document.
I attempted to write a query based on an example I found from the following blog:
SELECT c,t AS identifiers
FROM c
JOIN t in c.identifiers
WHERE t.identifierLabel = "someLabel2"
However, when the result is returned, it appends the following to the end of the document:
{
"name": {
"productName": "someProductName"
},
"identifiers": [
{
"identifierCode": "1234",
"identifierLabel": "someLabel1"
},
{
"identifierCode": "432",
"identifierLabel": "someLabel2"
}
]
},
{
"identifierCode": "432",
"identifierLabel": "someLabel2"
}
How can I avoid this and get the result that I desire, i.e. the entire document with nothing appended to it?
Thanks in advance.
Using ARRAY_CONTAINS(), you should be able to do something like this to retrieve the entire document, without any need for a self-join:
SELECT *
FROM c
where ARRAY_CONTAINS(c.identifiers, {"identifierLabel":"someLabel2"}, true)
Note that ARRAY_CONTAINS() can search for either scalar values or objects. By specifying true as the third parameter, it signifies searching through objects. So, in the above query, it's searching all objects in the array where identifierLabel is set to "someLabel2" (and then it should be returning the original document, unchanged, avoiding the issue you ran into with the self-join).

How to define an index to use in a Mango Query

I am trying to create a CouchDB Mango Query with an index with the hope that the query runs faster. At the moment I have the following Mango Query which returns what I am looking for but it's slow. Therefore, I assume, I need to create an index to make it faster. I need help figuring out how to create that index.
selector: {
categoryIds: {
$in: categoryIds,
},
},
sort: [{ publicationDate: 'desc' }],
You can assume that my documents are let say news articles from different categories. Therefore in each document I have a field that contains one or more categories that the news article belongs to. For that I have an array of categoryIds for each document. My query needs to be optimized for queries like "Give me all news that have categoryId1 in their array of categoryIds sorted by publicationDate". What I don't know how to do is 1. How to define an index 2. What that index should be 3. How to use that index in "use_index" field of the Mango Query. Any help is appreciated.
Update after "Alexis Côté" answer:
If I define the index like this:
{
"_id": "_design/0f11ca4ef1ea06de05b31e6bd8265916c1bbe821",
"_rev": "6-adce50034e870aa02dc7e1e075c78361",
"language": "query",
"views": {
"categoryIds-json-index": {
"map": {
"fields": {
"categoryIds": "asc"
},
"partial_filter_selector": {}
},
"reduce": "_count",
"options": {
"def": {
"fields": [
"categoryIds"
]
}
}
}
}
}
And run the Mango Query like this:
{
"selector": {
"categoryIds": {
"$in": [
"e0bd5f97ac35bdf6893351337d269230"
]
}
},
"use_index": "categoryIds-json-index"
}
It still does return the results but they are not sorted in the order I want by publicationDate. So I am not clear what you are suggesting the solution is.
You can create an index as documented here
In your case, you will need an index on the "categoryIds" field.
You can specify the index using "use_index": "_design/<name>"
Note:The query planner should automatically pick this index if it's compatible.

id cannot be used in graphQL where clause?

{
members {
id
lastName
}
}
When I tried to get the data from members table, I can get the following responses.
{ "data": {
"members": [
{
"id": "TWVtYmVyOjE=",
"lastName": "temp"
},
{
"id": "TWVtYmVyOjI=",
"lastName": "temp2"
}
] } }
However, when I tried to update the row with 'id' where clause, the console shows error.
mutation {
updateMembers(
input: {
values: {
email: "testing#test.com"
},
where: {
id: 3
}
}
) {
affectedCount
clientMutationId
}
}
"message": "Unknown column 'NaN' in 'where clause'",
Some results from above confused me.
Why the id returned is not a numeric value? From the db, it is a number.
When I updated the record, can I use numeric id value in where clause?
I am using nodejs, apollo-client and graphql-sequelize-crud
TL;DR: check out my possibly not relay compatible PR here https://github.com/Glavin001/graphql-sequelize-crud/pull/30
Basically, the internal source code is calling the fromGlobalId API from relay-graphql, but passed a primitive value in it (e.g. your 3), causing it to return undefined. Hence I just removed the call from the source code and made a pull request.
P.S. This buggy thing which used my 2 hours to solve failed in build, I think this solution may not be consistent enough.
Please try this
mutation {
updateMembers(
input: {
values: {
email: "testing#test.com"
},
where: {
id: "3"
}
}
) {
affectedCount
clientMutationId
}
}

How would I query keys such that it would partially match?

Let's take this document for example:
{
"id":1
"planet":"earth-616"
"data":[
["wolverine","mutant"],
["Storm","mutant"],
["Mark Zuckerberg","human"]]
}
I created a search index to index the name and type, for example if searched for name:wolverine or type:mutant I'd get the document that has it. But as per my requirement I don't want the whole document, I only want ["wolverine","mutant"] I've created a view that outputs as:
{
"id":1,
"key":"earth-616",
"value":["earth-616","wolverine","mutant"]
}
Then I found out I can query only with keys. (Is it possible to create search indexes on views?, Couldn't find anything in the documentation)
Or should I create views along with the one above like this:
{
"id":1,
"key":"wolverine",
"value":["earth-616","wolverine","mutant"]
}
And
{
"id":,
"key":"mutant"
"value":["earth-616","wolverine","mutant"]
}
This way I can query with keys that I want but I can't seem to partial match keys(Am I missing something?)
If you need the output to be exactly as described then I believe you have to use views, and to support wildcard searches I believe you will have to index every substring of a key.
One alternative is to use Cloudant Query, although admittedly you cannot get the exact output you are looking for. If you issue a query like so:
{
"selector": {
"_id": {
"$gt": 0
},
"data": {
"$elemMatch": {
"$elemMatch": {
"$regex": "(?i)zuck"
}
}
}
},
"fields": [
"data"
]
}
The result will be the entire data array:
{
"data": [
["wolverine", "mutant"],
["Storm", "mutant"],
["Mark Zuckerberg", "human"]
]
}

Query data where userID in multiples ID

I try to make a query and i don't know the right way to do this.
The mongo collection structure contains multiples user ID (uid) and i want to make a query that get all datas ("Albums") where the User ID match one of the uid.
I do not know if the structure of the collection is good for that and I would like to know if I should do otherwise.
{
"_id": ObjectId("55814a9799677ba44e7826d1"),
"album": "album1",
"pictures": [
"1434536659272.jpg",
"1434552570177.jpg",
"1434552756857.jpg",
"1434552795100.jpg"
],
"uid": [
"12814a8546677ba44e745d85",
"e745d677ba4412814e745d7b",
"28114a85466e745d677d85qs"
],
"__v": 0
}
I just searched on internet and found this documentation http://docs.mongodb.org/manual/reference/operator/query/in/ but I'm not certain that this is the right way.
In short, I need to know: if I use the right method for the stucture of the collection and the operator "$in" is the right solution (knowing that it may have a lot of "User ID": between 2 and 2000 maximum).
You don't need $in unless you are matching for more than one possible value in a field, and that field does not have to be an array. $in is in fact shorthand for $or.
You just need a simple query here:
Model.find({ "uid": "12814a8546677ba44e745d85" },function(err,results) {
})
If you want "multiple" user id's then you can use $in:
Model.find(
{ "uid": { "$in": [
"12814a8546677ba44e745d85",
"e745d677ba4412814e745d7b",
] } },
function(err,results) {
}
)
Which is short for $or in this way:
Model.find(
{
"$or": [
{ "uid": "12814a8546677ba44e745d85" },
{ "uid": "e745d677ba4412814e745d7b" }
]
},
function(err,results) {
}
)
Just to answer your question, you can use the below query to get the desired result.
db.mycollection.find( {uid : {$in : ["28114a85466e745d677d85qs"] } } )
However, you need to revisit your data structure, looks like its a Many-to-Many problem and you might need to think about introducing a mid collection for that.

Resources