Query Mongo collection for all documents with empty array nested in an array - mongodb-query

I have documents that look like this in a collection called movies:
{
"_id" : ObjectId("51c272623021490007000001"),
"movies": [
{
"name": "Booty Call"
"play_times": []
},
{
"name": "Bulletproof"
"play_times": [{...},{...}]
}
]
}
I would like to query for documents where "play_times" is not empty or null. Basically I only care about movies with play times.

If you want to query for separate array elements within documents, that is not possible AFAIK. If you want to get documents which have non-empty play_times, then use $size operator:
movies.play_times : { $size : { $gt : 0} }
To check if field exists, there is an $exists operator.

Related

How do I query referenced document fields within an array of objects in Sanity with GROQ?

I have a document which has an array of objects of which one of the fields is a reference to another document. The following query returns the referenced document _id and _type only, and I need other fields from those documents.
// GROQ query
*[slug.current == $slug]{
title,
slug,
objectArray
}
This results in:
"result": [
0: {
"title": "Test Document"
"slug": {
"_type": "slug"
"current": "test-document"
}
"objectArray": [
0: {...}
1: {
"_key": "583ec1dee738"
"_type": "documentObject"
"objectTitle" : "Test Object"
"documentReference": {
"_ref": "2f9b93b4-4924-45f2-af72-a38f7d9ebeb4"
"_type": "reference"
}
"booleanField": true
}
]
}
]
documentReference has its own set of fields (ie. title) in the schema which I need to be returned in my query.
How do I do this?
I have looked at the Sanity documentation at joins and object projections, but I cannot get the syntax right for when the reference is within an array of objects.
You need to do a join on the reference:
*[slug.current == $slug] {
title,
slug,
objectArray[] {
documentReference->
}
}
The syntax objectArray[] can be thought of as "for each element", and -> does a join to look up the referenced document. In other

Creating a couchdb view to index if item in an array exists

I have the following sample documents in my couchdb. The original table in production has about 2M records.
{
{
"_id": "someid|goes|here",
"collected": {
"tags": ["abc", "def", "ghi"]
}
},
{
"_id": "someid1|goes|here",
"collected": {
"tags": ["abc", "klm","pqr"]
},
},
{
"_id": "someid2|goes|here",
"collected": {
"tags": ["efg", "hij","klm"]
},
}
}
Based on my previous question here, how to search for values when the selector is an array,
I currently have an index added for the collected.tags field, but the search is still taking a long time. Here is the search query I have.
{
"selector": {
"collected.tags": {
"$elemMatch": {
"$regex": "abc"
}
}
}
}
There are about 300k records matching the above condition, there search seems to take a long time. So, I want to create a indexed view to retrieve and lookup faster instead of a find/search. I am new to couchdb and am not sure how to setup the map function to create the indexed view.
Figured the map function out myself. Now all the documents are indexed and retrievals are faster
function (doc) {
if(doc.collected.tags.indexOf('abc') > -1){
emit(doc._id, doc);
}
}

elasticsearch match two fields

How can I get this simple SQL query running on elasticsearch ?
SELECT * FROM [mytype] WHERE a = -23.4807339 AND b = -46.60068
I'm really having troubles with it's syntax, multi-match queries doesn't work in my case, which query type should I use?
For queries like yours bool filter is preferred over and filter. See here the whole story about this suggestion and why is considered to be more efficient.
These being said, I would choose to do it like this:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"term": {"a": -23.4807339}},
{"term": {"b": -46.60068}}
]
}
}
}
}
}
You can approach this with the and filter. For your example, something like:
{
"size": 100,
"query" : {"filtered" : {
"query" : {"match_all" : {}},
"filter" : {"and" : [
"term" : {"a": -23.4807339},
"term" : {"b": -46.60068}
]}
}}
}
Be sure to direct the query against the correct index and type. Note that I specified the size of the return set as 100 arbitrarily - you'd have to specify a value that fits your use case.
There's more on filtered queries here, and more on the and filter here.

elastic search query filter out ids by wildcard

I'm hoping to create a query where it will filter out IDs containing a wildcard. For instance, I would like to search for something everywhere except where the ID contains the word current. Is this possible?
Yes it is possible using Regex Filter/Regex Query. I could not figure a way to directly do it using the Complement option hence I've used bool must_not to solve your problem for the time being. I'll refine the answer later if possible.
POST <index name>/_search
{
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must_not": [
{
"regexp": {
"ID": {
"value": ".*current.*"
}
}
}
]
}
}
}

How do I sort ElasticSearch when it's empty?

Sometimes, I have nothing in the index, sometimes, I have some documents. That's just the nature of my application. When the index does contain documents, I sort by "final_score" descending. My query looks like this:
GET /_search
{
"query": {
"match_all":{}
},
"sort":[
{ "final_score" : "desc" }
]
}
However, this query breaks when there are 0 documents in the index. I would have to remove the sort to make the query work.
How can I make this query work with any amount of documents (0, or more?)
If you don't have field and ask elasticsearch to sort by that field then there is problem,
So,Have mapping for final_score, so that it will not throw error (if nothing is indexed also).
Example:
POST http://localhost:9200/index/type/_mapping
{
"type": {
"properties": {
"final_score": {
"type": "integer"
}
}
}
}