How do I search for the value "city"? - python-3.x

I'm trying to query through a somewhat simple collection with the following structure:
{
"name":[
{
"something":"",
"somethingelse":[
{
"name":"John",
"city":"NY"
}
]}]}
I have tried to search the value "city" with the dot notation but no success.

by reading this mongoDB doc, I see that to access array nested document you need to specify the index of the element instead of using ".".
For example with this object:
{
"name":"bob",
"info":[
{"birth":"10/01/1986"},
{"eyeColor":"blue"},
{"salary":2000}
]
}
I want to access the "birth" property, I will do something like this:
db.inventory.find({'info.0':"birth"})
Hope this help.

You can call the data and store it in an object say ob
ob= {
"name":[
{
"something":"",
"somethingelse":[
{
"name":"John",
"city":"NY"
}
]}]}
now you can do ob["name"][0]["somethingelse"][0]["city"]

Related

Pull from array which is in an array

I have a collection which contains a teams array and that contains a players array.
I would like to delete from the players array.
I think I know hot to delete from an array, but I can't make it work.
There was a case when it deleted all elements from the teams array.
Here it is how the doc look like:
"teams" : [
{
"_guid" : "5c5b3bc0-a957-11e5-b909-b7a1cbe2c8be",
"teamname" : "Ping-Win_team",
"_id" : ObjectId("567a68f6a7c726540b2d746b"),
"players" : [
ObjectId("567a68f6a7c726540b2d7469"),
ObjectId("567a68f7a7c726540b2d746c")
]
}
],
My probation:
db.lobbies.update({ _id: ObjectId('567a68f6a7c726540b2d746a') }, { $pull: { 'teams': { 'players.$': ObjectId('567a68f7a7c726540b2d746c') }}})
Thanks for helping,
Akos
Apply the $pull operator together with the $ positional operator in your update to change the name field. The $ positional operator will identify the correct element in the array to update without explicitly specifying the position of the element in the array, thus your final update statement should look like:
db.lobbies.update(
{ "teams.players": ObjectId("567a68f7a7c726540b2d746c") },
{
"$pull": {
"teams.$.players": ObjectId("567a68f7a7c726540b2d746c")
}
}
)
If I got correctly your question.. you can't pull one of the player IDs because:
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
From: https://docs.mongodb.org/v3.0/reference/operator/update/positional/
Otherwise you will pull the whole item of the teams array that contains that specific player.
You can use below code to delete one or multiple values in a nested array
db.sessions.update(
{
"teams": {
$elemMatch: {
"players": {$in: [ObjectId("567a68f7a7c726540b2d746c")]}
}
}},
{
"$pull": {
"teams.$.players": {$in:[ObjectId("567a68f7a7c726540b2d746c")]}
}
})

Elasticsearch - How to find out what is the type of a particular index

I created a document in ES. Later I want to query it, but I forget the type, therefore I can not complete the command. However I still remember the the index name.
How do I retrieve the type name of the index?
If you know the index name, you can quickly retrieve all the mapping types in your index using the following command:
curl -XGET 'http://localhost:9200/index_name/_mapping?pretty'
You'll get a response like the one below:
{
"index_name": {
"mappings": {
"type_name": { <----- this is the type name you're looking for
"properties": {
...field definitions...
}
}
}
}
}

$addToSet and return all new items added?

Is it possible to $addToSet and determine which items were added to the set?
i.e. $addToSet tags to a post and return which ones were actually added
Not really, and not with a single statement. The closest you can get is with the findAndModify() method, and compare the orginal document form to the fields that you submitted in your $addToSet statement:
So considering an initial document:
{
"fields": [ "B", "C" ]
}
And then processing this code:
var setInfo = [ "A", "B" ];
var matched = [];
var doc = db.collection.findAndModify(
{ "_id": "myid" },
{
"$addToSet": { "fields": { "$each": setInfo } }
}
);
doc.fields.forEach(function(field) {
if ( setInfo.indexOf(field) != -1 ) {
matched.push(field);
}
});
return matched;
So that is a basic JavaScript abstraction of the methods and not actually nodejs general syntax for either the native node driver or the Mongoose syntax, but it does describe the basic premise.
So as long as you are using a "default" implementation method that returns the "original" state of the document before it was modified the you can play "spot the difference" as it were, and as is shown in the code example.
But doing this over general "update" operations is just not possible, as they are designed to possibly affect one or more objects and never return this detail.

Finding embeded document in mongodb?

My collection is like this
{
"name":""
"type":""
"arr":[
{
"type":""
"other field"
...
},
{
"type":""
"other field"
...
}
}
and my condition is
input parameter is name.
so based on name i have to fetch document and one more condition is that type outside and inside the array should match..
Need to fetch those records alone..
How to achieve this
Bad performance, but just work (http://docs.mongodb.org/manual/reference/operator/where)
db.SOME_COLLECTION.find({
name:'SOME_VALUE',
$where:
function() {
for(var i = 0; i < obj.arr.length; i++) {
if(obj.arr[i].type==obj.type) {
return obj
}
}
}
}
)
p.s. aggregation framework has solved analogical problems, but in this case, imho, only $where clause available
If I understand you correctly, this is how you get the desired result
{name:"",type:"",arr.type:""}
you can put the desired values and it will return you all the matching record.

Searching term in subdocuments with elasticsearch

I have a index document structure like below;
{
"term":"some term",
"inlang":"some lang"
"translations" : {
{
"translation":"some translation",
"outlang":"some lang",
"translations" : {
{
"translation":"some translation 1"
"outlang": "some lang 1"
"translations" : {...}
}
}
},
...
}
}
I want to find a translation in such documents. However, this translation can exists at any level of this document. Is it possible to search term dynamically by using elasticsearch?
For example,
{
"query": {
"*.translation":"searchterm"
}
}
Thanks in advance
I have managed to do that with following query;
{
"query": {
"query_string": {
"query": "someterm",
"fields": ["*.translation"]
}
}
}
or
{
"query": {
"multi_match": {
"query": "someterm",
"fields": ["*.translation"]
}
}
}
You can see elasticsearch google group conversation here
No, I do not believe this functionality is built into ElasticSearch at the moment. This answer suggests you could build the functionality with a script, but it would be super slow.
In general, ES doesn't play nicely with nested data. It supports nested fields, but many of the more advanced search functionality isn't capable of operating on complex nested data. My suggestion is to denormalize your data so that every translation is represented by a single item in the index, and link between them with ID numbers.

Resources