Sort by keyword index mongodb
NickName:dahveed Ask DateTime:2021-08-08T11:04:52

Sort by keyword index mongodb

I was wondering if there was a way to sort my mongodb results by the index of the search keyword.
For example if I search the db by {name: da} i'll get back results like [{name: "yoda"}, {name: "david"}, {name: "soda"}, {name: "daniel"}]. But I would want the order to be something like this [{name: "david"}, {name: "daniel"}, {name: "yoda"}, {name: "soda"}]. I know that I can sort the results after I find them like this:

const re = new RegExp(keyword, "i");
results.sort((a, b) => a.name.search(re) - b.name.search(re)

But I also wanted to limit the amount of documents I get back from the database. I looked at the textScore metadata in the mongodb docs, but I have no clue if its useful in my case.

Copyright Notice:Content Author:「dahveed」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68697479/sort-by-keyword-index-mongodb

More about “Sort by keyword index mongodb” related questions

Sort by keyword index mongodb

I was wondering if there was a way to sort my mongodb results by the index of the search keyword. For example if I search the db by {name: da} i'll get back results like [{name: "yoda"},...

Show Detail

mongodb index find index sort

I would like to ask about mongodb indexes. Can I use a different index in the find and the sort. By example I have two indexes: (a:-1) (b:1,c:1) What indexes uses this sentence? ({a:...

Show Detail

Is it possible to pre-sort a text index in MongoDB?

My understanding is that, in MongoDB, regular (not text) indexes are pre-sorted based on the parameters passed to createIndex(). For example, db.collection.createIndex({ name: 1 }) will create an i...

Show Detail

CosmosDB MongoDB 3.6 fails sort() query with compounded index

Newby MongoDB & CosmosDB user here, I've read the answer to this question How does MongoDB treat find().sort() queries with respect to single and compound indexes? and the offocial MongoDB docs...

Show Detail

Elasticsearch Query parameter with sort keyword

I have been using ES 5.4.0 version and created index and documents (student data). when i do search based on age , _search end point is returning value and output is as expected. Did the same for n...

Show Detail

Mongodb compound index - also built in _id sort?

I have a compound index { userID:1, connectionStatus: 1, userTargetLastName: 1}) I want to support two queries: UserConnection.find( { $and : [ { userID : req.decoded.id }, { connectionStatus : '

Show Detail

MongoDB C# Case Insensitive Sort and Index

So far I've been using this code to find my documents and then sort them: var options = new FindOptions { Modifiers = new BsonDocument("$hint", "PathTypeFilenameIndex")...

Show Detail

mongodb sort with multiple fields

In mangoDB -3.0.7 i have created a index with 2 fields. I can run the sort on those fileds When i give only ascending/descending. Example { a: 1, b: 1 } But i when i run query like, { a: 1, b:...

Show Detail

How to sort array by index value in mongodb

Hi i have some question what i want to achieve is to sort progress in index 1 how can i do it with mongodb? i try some code but cannot achieve it { "fb_id" : "user1", "progress" : [ 1, 20 ] } { "...

Show Detail

Sort MongoDB query by additional keyword

I am building a REST Api on Flask + MongoDB for a HR website. I need to filter search results by programming language, experience, city etc. I also have a filter 'framework'. But I also need to imp...

Show Detail