Elasticsearch : Set the number of the elements of an array in a field - arrays

I'm trying to store the number of elements of an array into a field with an update. (updating the record with the count of the array).
Like for example
{
array = [ { "a" : "b" }, { "c" : "d" } ]
nb_elements = 2
}
I have tried an update with the following script
{
"script" : "ctx._source.nb_elements= ctx._source.array.values.length"
}
But it don't work.
I also tried :
{
"script" : "ctx._source.nb_elements = count",
"params" : {
"count" : ctx._source.array.values.length
}
}
But i wasn't more successfull.
Does anybody know if this is possible and if, how to do it ?

First of all you need to enable dynamic scripting by adding script.disable_dynamic: false to your config file.
Secondly, I'm supposing that you are using groovy (It's the default scripting language from 1.4).
Using the update api with this script should work:
{
"script": "ctx._source.nb_elements=ctx._source.array.size"
}

Related

Elasticsearch | Query over array of objects

Lets say I have an object in ES such as:
{
name: "calendar";
events: [
{
birthday: "1992-10-09",
graduation: "2018-06-15",
wedding: "2016-12-12"
}
]
}
Is there a way I can query over the events array to find an element in the events array that is passed the current date.
So far I have:
GET /index/type/_search
{
"query": {
"range" : {
"events" : {
"gte" : "now"
}
}
}
}
but that does not iterate over each object in the array and return the name of the event. For this scenario it should return the object graduation: "2018-06-15". Thanks for the help!
You can use Nested datatypes to store nested documents inside parent document.
And to query back the Nested types you can use Nested Query.

Updating a collection inside array of collections in MongoDB

I have a mongo collection workspaces in my MongoDB which consist of records that looks like as follows:
{
"_id" : ObjectId("58bdc4a13504f5ed743ad025"),
"name" : "My workspace",
"user_id" : ObjectId("58b6cf53988a874af070fd3b"),
"uploads" : [
{
"original" : "DE-20__450_GG_5002_N23_994__0136.tif"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0134.png"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0136.png"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0134.tif"
}
]
}
Now my objective is that i want to append something in one of record of array uploads with the match criteria of original field value. For example i want to append "pseg" : "DE-20__450_GG_5002_N23_994__0136.pseg.tif" to the {"original" : "DE-20__450_GG_5002_N23_994__0136.tif"} collection in following way:
{
"_id" : ObjectId("58bdc4a13504f5ed743ad025"),
"name" : "Objective",
"user_id" : ObjectId("58b6cf53988a874af070fd3b"),
"uploads" : [
{
"original" : "DE-20__450_GG_5002_N23_994__0136.tif",
"pseg" : "DE-20__450_GG_5002_N23_994__0136.pseg.tif"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0134.png"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0136.png"
},
{
"original" : "DE-20__450_GG_5002_N23_994__0134.tif"
}
]
}
i tried using $elemMatch and then i added $addToSet in following way:
db.getCollection('workspaces').update({"_id": ObjectId("58bdc4a13504f5ed743ad025"),"uploads": {$elemMatch: {"original": "DE-20__450_GG_5002_N23_994__0136.tif"}}},{$addToSet:{"uploads.$": {"pseg" : "DE-20__450_GG_5002_N23_994__0136.pseg.tif"}}})
but it gives me following error:
Cannot apply $addToSet to a non-array field. Field named '0' has a non-array type object in the document _id: ObjectId('58bdc4a13504f5ed743ad025')
I think i crafted the query wrong can anyone tell me what is wrong and how can i solve it?
Sorry for my bad english :) hope you understood what i said.
Here is the correct syntax. You have to use $set with postional operator to reach the element.
db.getCollection('workspaces').update(
{"_id": ObjectId("58bdc4a13504f5ed743ad025"),"uploads": {$elemMatch: {"original": "DE-20__450_GG_5002_N23_994__0136.tif"}}},
{$set:{"uploads.$.pseg" : "DE-20__450_GG_5002_N23_994__0136.pseg.tif"}})
More information here https://docs.mongodb.com/manual/reference/operator/update/positional/#update-documents-in-an-array
You can simplify your query criteria. You don't need to use $elemMatch operator for single query condition. You can use dot notation.
db.getCollection('workspaces').update(
{"_id": ObjectId("58bdc4a13504f5ed743ad025"),"uploads.original": "DE-20__450_GG_5002_N23_994__0136.tif"},
{$set:{"uploads.$.pseg" : "DE-20__450_GG_5002_N23_994__0136.pseg.tif"}})
More information here
https://docs.mongodb.com/manual/reference/operator/query/elemMatch/#single-query-condition

How get time on timecurrent on node of Firebase using Angular?

I want to show only the time on the view, when use $firebaseArray I get the list of items and i want to get only the time.
Firebase tree
{
"eventmodel" : {
"-KX6kDkufxLg-fLocsN7" : {
"info" : "ok",
"limit" : 2,
"timeCurrentStart" : "29/11/2016 17:38",
},
"-KXBB-R7xAPl65xhlTpg" : {
"info" : "ok",
"limit" : 2,
"timeCurrentStart" : "29/11/2016 17:38",
}
}
}
The Firebase client always retrieves complete nodes. There is no way to get only the timeCurrentStart property of each node.
So either you'll have to store the timeCurrentStart properties in a separate node:
{
"eventmodeltimes" : {
"-KX6kDkufxLg-fLocsN7" : "29/11/2016 17:38",
"-KXBB-R7xAPl65xhlTpg" : "29/11/2016 17:38"
}
}
Or you'll have to retrieve the entire nodes and only show timeCurrentStart:
<span>{{event.timeCurrentStart}}</span>

How to find documents in MongoDb matching a field and subdocument field that are in an array

The document structure is as follows:
{
"_id" : "V001-99999999",
"vendor_number" : "V001",
"created_time" : ISODate("2016-04-26T22:15:34Z"),
"updated_time" : ISODate("2016-06-07T21:45:46.413Z"),
"items" : [
{
"sku" : "99999999-1",
"status" : "ACTIVE",
"listing_status" : "LIVE",
"inventory" : 10,
"created_time" : ISODate("2016-05-14T22:15:34Z"),
"updated_time" : ISODate("2016-05-14T20:42:21.753Z"),
},
{
"sku" : "99999999-2",
"status" : "INACTIVE",
"listing_status" : "LIVE",
"inventory" : 10,
"created_time" : ISODate("2016-04-26T22:15:34Z"),
"updated_time" : ISODate("2016-06-06T20:42:21.753Z"),
}
]
}
I want to obtain the sku from the item, the conditions are:
1) "vendor_number" = "XXX"
2) items.status = "ACTIVE" AND items.updated_time < [given_date]
Result example:
"sku" : "99999999-2"
or csv:
"sku","99999999-2"
Thank you for your support.
This should be what you want. Although I'm assuming you wanted "status": "active"?
db.getCollection('collection').aggregate([
{ $match: { "vendor_number": "XXXX" } },
{ $project: {
"items": {
$filter: {
input: "$items",
as: "item",
cond: { $eq: ["$$item.status", "ACTIVE"] } // or maybe ["$$item.listing_status", "LIVE"] ?
}
}
}
},
{ $project: { "items.sku": true } }
])
I love using aggregation to manipulate stuff. It's great all the things you can do with it. So here's what's going on:
The first part is simple. The $match step in the aggregation pipeline says just give me documents where vendor_number is "XXXX".
The next part is a bit hairy. The first projection step creates a new field, called "items", I could have called it "results" or "bob" if I wanted to. The $filter specifies which items should go into this new field. The new "items" field will be an array that will have all the results from the previous items field, hence the input: "$items", where you're using the keyword "item" to represent each input item that comes into the filter. Next, the condition says, for each item, only put it in my new "items" array if the item's status is "ACTIVE". You can change it to ["$$items.listing_status", "LIVE"] if that's what you needed. All of this will pretty much give you you're result.
The last project just get's rid of all other fields except for items.sku in each element in the new "items" array.
Hope this help. Play around with it and see what else you can do with the collection and aggregation. Let me know if you need any more clarification. If you haven't used aggregation before, take a look at the aggregation docs and the list of pipeline operators you can use with aggregation. Pretty handy tool.

Mongodb error: The positional operator did not find the match needed from the query [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 6 years ago.
I have a collection with objects like this
{
"_id" : ObjectId("5742be02289512cf98bf63e3"),
"name" : "test1",
"attributes" : [
{
"name" : "x",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e8")
},
{
"name" : "y",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e7")
},
{
"name" : "z",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e6")
}
],
"__v" : 6
}
And I want to update all documents, and set for each attribute new field.
So I want to run a single query to update all documents at once. I think, this query will do
db.spaces.update({}, { $set: { "attributes.0.weight": 2 } }, {multi: true})
But when I run this query, I get an error:
"code" : 16837,
"errmsg" : "The positional operator did not find the
match needed from the query. Unexpanded update: attributes.$.weight"
I can't understand why.
Please help
You need to include the array field as part of the query document in order to use the positional operator.
For example, if you want to update the first array element i.e. with { "attributes.name": "x" } then you could follow the pattern:
db.spaces.update(
{ "attributes.name": "x" }, // <-- the array field must appear as part of the query document.
{ "$set": { "attributes.$.weight": 2 } },
{ "multi": true }
)
For the newer MongoDB versions 3.2.X, you could use the updateMany() method to update multiple documents within the collection based on the filter above.
The positional operator needs a match, from the match part of your update query.
for example:
db.spaces.update({ "attributes.name": "x" }, { $set: { "attributes.0.weight": 2 } }, {multi: true})
here the first parameter for update operation will match the array attributes where any element has a property name=="x", for any element that matches the condition the position operator can be used to update it.
So, because name='x', in this case the first matching element would be,
{
"name" : "x",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e8")
},
and it will get updated.
Now from your question I understand you want to update the document in a way that in each document your first element, attribute gets a new value for weight=2.
you can do something like
db.spaces.update({ "attributes.name": { $regex: /^(?=[\S\s]{10,8000})[\S\s]*$/ } }, { $set: { "attributes.0.weight": 2 } }, {multi: true})
What we do here is match all element in array attribute. and we use the positional operator to update the first element of that array

Resources