RethinkDB simple filter not working? - rethinkdb

I'm learning ReQL in the web UI "Data Explorer" and have created the following "cars" table with 2 documents, in the provided "test" database:
[{
"brand": "Nissan" ,
"id": 1 ,
"model": "Murano" ,
"year": 2009
} ,
{
"brand": "Nissan" ,
"id": 2 ,
"model": "Qashqai" ,
"year": 2014
}
]
While the following query returns both documents correctly:
r.table("cars")
...the following should return only the second document but why does it instead return an empty array?:
r.table("cars").filter(
r.row["year"] > 2010
)
I got this filter query straight out of the official samples at http://www.rethinkdb.com/docs/sql-to-reql/

The examples in the SQL to ReQL cheat sheet are in Python.
However the Data Explorer uses JavaScript. In JavaScript, .gt must be used instead of > and () instead of []:
r.table("cars").filter(r.row("year").gt(2010))

Related

how to retrieve the index name when translating SQL query in elasticsearch/opensearch

According to the official doc of elasticsearch/opendistro, the SQL translate API could translate SQL query into elasticsearch DSL. However, it only returns columns, filters, size and sorting operators without any index/table name.
E.g., given sql query SELECT * FROM library LIMIT 2, the SQL translate API will respond:
{
"size": 2,
"_source": false,
"fields": [
{
"field": "author"
},
{
"field": "name"
},
{
"field": "page_count"
},
{
"field": "release_date",
"format": "strict_date_optional_time_nanos"
}
]
}
And the index name (library) is missing. I tried opendistro and it is the same.
Notice that for aggregated query across two table/index, it will return Physical Plan and Logical Plan that contains TableScan attributes.
Currently Elasticsearch not support to return index name for Translate API. There is already open Github issue #41856.
It will return you only query source and you need to execute translate source with same index name which you have used in SQL otherwise it will be throw error or return unexpected result as mentioned in #34594 GitHub issue.

How does changes() work in rethinkdb?

Example from the home page of rethinkdb.com doesn't work as expected.
r.db("test").tableCreate("game");
r.db("test").table("game").indexCreate("score");
r.db("test").table("game").insert({name: "brandon", score: 60});
r.db("test").table("game").insert({name: "leon", score: 80});
r.db("test").table("game").insert({name: "connor", score: 100});
r.db("test").table("game").orderBy({index: "score"}).limit(3).changes()
Output:
{ "new_val": { "id": "c727b9eb-5aaa-46f9-bc09-a6c879cfbfa0" , "name":
"brandon" , "score": 60 } } { "new_val": { "id":
"b59d4314-b78c-48c9-8780-0f9d3a6b6887" , "name": "leon" , "score": 80
} } { "new_val": { "id": "519343b1-cd98-4969-8f07-7bff5d981c81" ,
"name": "connor" , "score": 100 } }
r.db("test").table("game").insert({name: "mike", score: 70});
Nothing changes but must be changed due to ordering by score.
r.db("test").table("game").get("519343b1-cd98-4969-8f07-7bff5d981c81").update({score: 50}) // {name: "connor"}
Still nothing..
So why ordered list is not updates as it should be?
This is a bug in the data explorer, unfortunately. It was fixed in https://github.com/rethinkdb/rethinkdb/issues/4852 and the fix will be pushed out as a point release soon. Until it's released I'd recommend using one of the drivers to test these queries instead.
This appears to be a failure of the Web interface. Running these commands from a client driver (I tried it with the JavaScript driver on 2.1.3 and 2.1.4) shows the changes as expected, it's just that the Data Explorer does not update correctly. If you swap tabs to Table View and back you can see that the cursor has received the changes at the bottom.

Mongodb Java nested update

I have a MongoDB document structure like this
{
"_id": "002",
"list": [
{
"year": "2015",
"entries": [{...}, {...}]
},
{
"year": "2014",
"entries": [{...}, {...}]
}
]
}
I want to push a new element into "entries". I know it is possible using
collection.updateOne(
Filters.eq("_id", "002"),
new Document("$push", new Document("list.0.entries", "{...}")
);
But this appends to "entries" of the 1st element of "list". I want to append to "entries" for the "year" 2015. How can I do this with MongoDB Java driver API (3.0)?
I think you should use something like
Filters.and(Filters.eq("_id", "002"), Filters.eq("list.year", "2015"))
PS As the Filters javadoc suggests, it's convenient to use static import for it (to make it less verbose by skipping the "Filters." part)
You can use
Bson filter = Filters.and(Filters.eq("_id", "002"), Filters.eq("list", Filters.eq($elemMatch, Filters.eq("year", "2015"))
Document list = collection.find().filter(filter)
Afterwards you can iterate through the list to find the year 2015 and get the entries for this one and insert the new element via java code. Keep the updated list in a local variable and write this one through an update command into your mongoDB.

Count and flatten in pig

Hi I have a data like this :
{"user_id": "kim95", "type": "Book", "title": "Modern Database Systems: The Object Model, Interoperability, and Beyond.", "year": "1995", "publisher": "ACM Press and Addison-Wesley", "authors": [{"name":"null"}], "source": "DBLP"}
{"user_id": "marshallo79", "type": "Book", "title": "Inequalities: Theory of Majorization and Its Application.", "year": "1979", "publisher": "Academic Press", "authors": [{"name":"Albert W. Marshall"},{"name":"Ingram Olkin"}], "source": "DBLP"}
{"user_id": "knuth86a", "type": "Book", "title": "TeX: The Program", "year": "1986", "publisher": "Addison-Wesley", "authors": [{"name":"Donald E. Knuth"}], "source": "DBLP"}
...
And I would like to get the publisher,title and then applied a count on the group but I got error ' a column needs to be...' with this script :
books = load 'data/book-seded-workings-reduced.json'
using JsonLoader('user_id:chararray,type:chararray,title:chararray,year:chararray,publisher:chararray,authors:{(name:chararray)},source:chararray');
doc = group books by publisher;
res = foreach doc generate group,books.title,count(books.publisher);
DUMP res;
On a second query I would like to have a structure like this :(name,year),title
So I tried this one :
books = load 'data/book-seded-workings-reduced.json'
using JsonLoader('user_id:chararray,type:chararray,title:chararray,year:chararray,publisher:chararray,authors:{(name:chararray)},source:chararray');
flat =group books by (generate FLATTEN((authors.name),year);
tab = foreach flat generate group, books.title;
DUMP tab;
But it also doesn't work...
Any idea please?
What is the error you are getting on trying out the first query?
COUNT being inbuilt function has to be in all caps, you cannot invoke COUNT(group), group is internal identifier generated by Pig.
I get following result on running your first query -
(Academic Press,{(Inequalities: Theory of Majorization and Its Application.)},1)
(Addison-Wesley,{(TeX: The Program)},1)
(ACM Press and Addison-Wesley,{(Modern Database Systems: The Object Model, Interoperability, and Beyond.)},1)
The expected format of (name,year), title can also be achieved this way -
flat = foreach books generate FLATTEN(authors.name) as authorName, year, title;
tab = group flat by (authorName, year);
finaltab = foreach tab generate group, flat.title;
Only problem in your first code i could see is "COUNT" instead of count (CAPS on)
if you use without caps count then you will get a error
Could not resolve count using imports:

Find matching array items in MongoDB document

I am developing a web app using Codeigniter and MongoDB.
In the database I got a document that look like this:
{
"_id": {
"$id": "4f609932615a935c18r000000"
},
"basic": {
"name": "The project"
},
"members": [
{
"user_name": "john",
"role": "user",
"created_at": {
"sec": 1331730738,
"usec": 810000
}
},
{
"user_name": "markus",
"role": "user",
"created_at": {
"sec": 1331730738,
"usec": 810000
}
}
]
}
I need to search this document using both user_name and role. Right now when I am using the below code I get both. I only want to get array items matching both user_name and role.
$where = array (
'_id' => new MongoId ($account_id),
'members.user_id' => new MongoId ($user_id),
'members.role' => $role
);
$this -> cimongo -> where ($where) -> count_all_results ('accounts');
This is an old question, but as of MongoDB 2.2 or so you can use the $ positional operator in a projection so that only the matched array element is included in the result.
So you can do something like this:
$this->cimongo->where($where)->select(array('members.$'))->get('accounts');
This is a repeat of this question:
Get particular element from mongoDB array
Also you might want to use $elemMatch
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray
Here is the rub -- you aren't going to be able to get the array items that match because mongo is going to return the entire document if those elements match. You will have to parse out the code client side. Mongo doesn't have a way to answer, "return only the array that matches."

Resources