How do you write a query that returns an ordered list starting from a specific item? - linq

If you have table with {1,3,4,5,9,15,43} how do you write a query that returns the three items following "5" in the natural sort order?

db.table
.Where(x=>x.field>5)
.OrderBy(x=>x.field)
.Take(3);

Related

Get n-th result returned from MATCH query in Neo4J database

I've set up a library database where users borrow books. Using a MATCH Command i can return the book titles and number of their lendings by descending order.
My Cypher for returning the list of books and number of lendings is:
MATCH (user)-[:LENDING]->(b:Book)
RETURN b.title, COUNT(b.title) as numberOfRents
ORDER BY numberOfRents DESC
This is working properly. However, i need to get the n-th book(by lendings) returned only(let's say the third for example), which is something i failed to do until now.
Sounds like you need SKIP and LIMIT
MATCH (user)-[:LENDING]->(b:Book)
RETURN b.title, COUNT(b.title) as numberOfRents
ORDER BY numberOfRents DESC
SKIP 2 LIMIT 1
// skips the first 2, so you only get the 3rd

Cloudant couchdb query custom sort

I want to sort the results of couchdb query a.k.a mango queries based on custom sort. I need custom sort because if Status can be one of the following:
Active = 1
Sold = 2
Contingent = 4
Pending = 3
I want to sort the results on Status but not in an alphabetical order, rather my own weightage I assign to each value which can be seen in the above list. Here's the selector for Status query I'm using:
{type:"Property", Status:{"$or":[{$eq: "Pending"}, {$eq:"Active"}, {$eq: "Sold"}]}}
If I use the sort array in my json with Status I think it'll sort alphabetically which I don't want.
You are actually looking for results based on "Status". You can create a view similar to this:
function(doc) { if (doc.type == "Property") { emit(doc.Status, doc);}}
When you use it, invoke it 4 times in the order you need and you'll get the result you need. This would eliminate the need to sort.

ElasticSearch sort by id's in array

Is there a way to sort some elasticsearch response in the same direction, which I am posting an array with ids?
Example: array[23,45,67] and the results should be sort in the same way like the id's are: first come all rows with ID 23, after that all rows with ID 45 and at the end all rows with ID 67 ?
Thanks
Nik
You can use scripting in sort or the other option is to use bool - should query where you boost documents with these values.

How to retrieve the last 100 documents with a MongoDB/Moped query?

I am using the Ruby Mongoid gem and trying to create a query to retrieve the last 100 documents from a collection. Rather than using Mongoid, I would like to create the query using the underlying driver (Moped). The Moped documentation only mentions how to retrieve the first 100 records:
session[:my_collection].find.limit(100)
How can I retrieve the last 100?
I have found a solution, but you will need to sort collection in descending order. If you have a field id or date you would do:
Method .sort({fieldName: 1 or -1})
The 1 will sort ascending (oldest to newest), -1 will sort descending (newest to oldest). This will reverse entries of your collection.
session[:my_collection].find().sort({id:-1}) or
session[:my_collection].find().sort({date:-1})
If your collection contain field id (_id) that identifier have a date embedded, so you can use
session[:my_collection].find().sort({_id:-1})
In accordance with your example using .limit() the complete query will be:
session[:my_collection].find().sort({id:-1}).limit(100);
Technically that query isn't finding the first 100, that's essentially finding 100 random documents because you haven't specified an order. If you want the first then you'd have to say explicitly sort them:
session[:my_collection].find.sort(:some_field => 1).limit(100)
and to reverse the order to find the last 100 with respect to :some_field:
session[:my_collection].find.sort(:some_field => -1).limit(100)
# -----------------------------------------------^^
Of course you have decide what :some_field is going to be so the "first" and "last" make sense for you.
If you want them sorted by :some_field but want to peel off the last 100 then you could reverse them in Ruby:
session[:my_collection].find
.sort(:some_field => -1)
.limit(100)
.reverse
or you could use use count to find out how many there are then skip to offset into the results:
total = session[:my_collection].find.count
session[:my_collection].find
.sort(:some_field => 1)
.skip(total - 100)
You'd have to check that total >= 100 and adjust the skip argument if it wasn't of course. I suspect that the first solution would be faster but you should benchmark it with your data to see what reality says.

How to sort by two values in MongoDB-Ruby?

So I have a Ruby Script where I find all the documents with the type "homework" in the "grades" collection a "students" DB (MongoDB) The thing is, following these instructions:
http://api.mongodb.org/ruby/current/file.TUTORIAL.html
I try to sort by score and then by student id (or viceversa) with:
homeworks.sort(:score, 1).sort(:student_id, 1).to_a
And running the file ("mongo.rb") I get an output of homeworks sorted by score (ascending) but not by student ID... (They're scrambled) If I try to switch values, I get the array ordered by student_id (ascending) but not by score... (In that case, score values are scrambled)
How can I Sort ascending by two arguments in mongo using ruby??
Per documentation, try
homeworks.sort([[:score, 1], [:student_id, 1]]).to_a
How about this:
c = db['grades']
x = c.find({}, {:sort=>[[:student_id, 1], [:score, 1]]}).to_a
This works for me in irb.

Resources