Elasticsearch filter on aggregation - database

Since I couldn't find anything on Google:
Is it possible to filter on an aggregation in elasticsearch. Im thinking of someting like: Get all objects where SUM(obect.X) > 100.
Thanks in advance.
EDIT - Sample Data
I have the following document-structure:
{
docKey : 1
value: 2
},
{
docKey: 1
value: 5
},
{
docKey:1
value: 7
},
{
docKey:2
value:2
},...
I now want to query the docKey from the database where the sum of the value is bigger than X.
EDIT2
I found out that is something I have to do in my application logic.
you can see it here

This doesn't exactly answer your question but seeing as you were pretty vague i.e. posting no mapping, query or anything of that matter we can't help you. However I hope you'll be able to work it out for yourself with this fantastic article on aggregations. Best of luck.
Update - Maybe this is what you're looking for instead?

Related

Why can't i get the variable value using $concat

As you can see in the following query i'm trying to update the titles of all the movies that got directors as Tom Hanks and i want the title to look like : title->tom hanks
db.movies.updateMany({"directors":"Tom Hanks"},{$set:{"title":{$concat:["$title","->","Tom Hanks"]}}});
But what i got instead is the following :
title: { '$concat': [ '$title', '->', 'Tom Hanks' ] }
what i understood that it doesn't consider it as variable i've looked in many website but i didn't find any answer i hope you can help

MongoDB find in array in all documents

I have a collection of documents with this simplified structure:
{
cards: [
{
id: "some string",
....
},{...}{...}{...}
]
}
I need to query the whole collection of documents and find those documents where my defined ID of a card matches one of the objects in the array of cards.
I usually fetch all documents and do it locally but this is quite large collection and I have no clue how to achieve this if even possible.
Thank you for your time!
ps: I'm using nodejs mongoose
After playing around with mohammad Naimi's solution I figured the correct syntax:
db.collection.find({ cards:{ $elemMatch: { id: { $eq:"some string" }}}})
db.collection.find({cards:{$elemMatch:{id:{$eq:"some string"}}})

looking for query

Im trying to find me based on city.(not using lat and long).
i have following function but as a newbie to gremlin im not sure is a right approach or not.
async function addRestfgaurant(restaur) {
// to add restaurants
after adding restaurant will do the same while adding Person with edlivesIn then will try to query restaurants near me. (im trying to make dining by friend api)
i want to Know is it right approach or not? or if someone has example of this type of query it would be really helpful.
What you are doing is reasonable but you can simplify things a bit. By using as steps to label earlier parts of the query you can then refer to them in the from and to parts. Note that in my example I also changed it to be all one query rather than three.
async function addRestaurant(restaurant:InputRestaurant) {
// to add restaurants
await g.addV("Restaurant").as('r').
property("restaurantId", restaurant.restaurantId).
property("name", restaurant.name).
property("city", restaurant.city).
property("street", restaurant.street).next()
// to add city
.addV("city").property("cityName", restaurant.city).as('c').
//edge
.addE("withIn")
.from_('r')
.to('c')).next()
return {
name: restaurant.name,
restaurantId: restaurant.restaurantId,
city: restaurant.city,
street: restaurant.street,
}

Meteor/Mongo get a specific document in an array

Considering:
{
docs: Array[{field1, field2}]
}
I know how to find the document(s) containing field1 or field2 with $elemMatch, but my question is how can I get the value of field1 knowing field2?
I also know that in MongoDB you would use $elemMatch in the projection parameter. Or that I can do this in JS with something like _.find(), but the goal is to get only one specific document from Mongo.
Let say for example a document:
{
_id: 1
docs: [{a: 42, b:"stringX"}, {a:0, b:"stringY"}]
}
How can I get the value of a (42) knowing b ("stringX")?
Is there something like: MyCollection.findOne({_id: 1}, projection: {docs:{b: "stringX"}}) ?
On the server you can use this projection:
var a = MyCollection.findOne({'docs.b': "stringX"},{fields: {'docs.$': 1}}).a;
However the $ array projection does not work on the client in minimongo (yet).
Hopefully this is sufficient to get you unstuck.
See related question

Using CouchDB-lucene how can I index an array of objects (not values)

Hello everyone and thanks in advance for any ideas, suggestions or answers.
First, the environment: I am using CouchDB (currently developing on 1.0.2) and couchdb-lucene 0.7. Obviously, I am using couchdb-lucene ("c-l" hereafter) to provide full-text searching within couchdb.
Second, let me provide everyone with an example couchdb document:
{
"_id": "5580c781345e4c65b0e75a220232acf5",
"_rev": "2-bf2921c3173163a18dc1797d9a0c8364",
"$type": "resource",
"$versionids": [
"5580c781345e4c65b0e75a220232acf5-0",
"5580c781345e4c65b0e75a220232acf5-1"
],
"$usagerights": [
{
"group-administrators": 31
},
{
"group-users": 3
}
],
"$currentversionid": "5580c781345e4c65b0e75a220232acf5-1",
"$tags": [
"Tag1",
"Tag2"
],
"$created": "/Date(1314973405895-0500)/",
"$creator": "administrator",
"$modified": "/Date(1314973405895-0500)/",
"$modifier": "administrator",
"$checkedoutat": "/Date(1314975155766-0500)/",
"$checkedoutto": "administrator",
"$lastcommit": "/Date(1314973405895-0500)/",
"$lastcommitter": "administrator",
"$title": "Test resource"
}
Third, let me explain what I want to do. I am trying to figure out how to index the '$usagerights' property. I am using the word index very loosely because I really do not care about being able to search it, I simply want to 'store' it so that it is returned with the search results. Anyway, the property is an array of json objects. Now, these json objects that compose the array will always have a single json property.
Based on my understanding of couchdb-lucene, I need to reduce this array to a comma separated string. I would expect something like "group-administrators:31,group-users:3" to be a final output.
Thus, my question is essentially: How can I reduce the $usagerights json array above to a comma separated string of key:value pairs within the couchdb design document as used by couchdb-lucene?
A previous question I posted regarding indexing of tagging in a similar situation, provided for reference: How-to index arrays (tags) in CouchDB using couchdb-lucene
Finally, if you need any additional details, please just post a comment and I will provide it.
Maybe I am missing something, but the only difference I see from your previous question, is that you should iterate on the objects. Then the code should be:
function(doc) {
var result = new Document(), usage, right;
for(var i in doc.$usagerights) {
usage = doc.$usagerights[i];
for(right in usage) {
result.add(right + ":" + usage[right]);
}
}
return result;
}
There's no requirement to convert to a comma-separated list of values (I'd be intrigued to know where you picked up that idea).
If you simply want the $usagerights item returned with your results, do this;
ret.add(JSON.stringify(doc.$usagerights),
{"index":"no", "store":"yes", "field":"usagerights"});
Lucene stores strings, not JSON, so you'll need to JSON.parse the string on query.

Resources