How to sort by string

Hello,
I am using sample_airbnb database with listingsAndReviews collection. I would like to use the string field
room_type:“Entire home/apt” to sort my query. Could you please let me know my error in the following script:

db.listingsAndReviews.find().sort({room_type:1}).collation({ locale: “en”, caseLevel: true }).pretty()

MongoServerError: Executor error during find command :: caused by :: Sort exceeded memory limit of 33554432 bytes, but did not opt in to external sorting.

Thanks

Hi @john_Doe3,
Each document in the sample_airbnb’s listingAndReviews collection has around 45 fields.
Since with more than 5.6k documents and an average size of 17KB/document it would take a lot of memory and time to sort the results.
Do you need all the 45 fields that are there? If not, a projection on the fields you need might be helpful in your case.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like

Hi, thank you so much for your reply.
I added projection without success. Could you please let me know the error.
I only need to show some results.

db.listingsAndReviews.find({projection:{ interaction: 1, name: 1, _id: 0 }}).sort({room_type:1}).collation({ locale: "en", caseLevel: true }).pretty()

Thanks again!

Hi @john_Doe3,

That’s not the correct syntax to perform projection on the results.
Try the following instead:

db.listingsAndReviews.find(
    { }, 
    { interaction: 1, name: 1, _id: 0 }
)
.sort({ room_type:1 })
.collation({ locale: "en", caseLevel: true })
.pretty()

Where

  • The first object {} passed to find() is used to filter the results based on the expression, here we have used an empty object, therefore all the documents in the collection will be returned.
  • And the second object { interaction: 1, name: 1, _id: 0 } is used to project the fields that we want in the results, therefore only interaction, name, and _id will be returned in resulted documents.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like

Hi Sourahb, many thanks. I still get the same error. However, when I added limit of 500, it works properly. Adding a limit of 5000 wont work. Thanks again for your help!
have a good one,

Dear Sourabh,
I created the following collection (test) and would to use $graphLookup. I was wondering if by chance you can tell me what is the error?

db.test.insert([ { node:10, connectedTo: [40]}, {node:20, connectedTo:[30,40]}, {node:30, connectedTo:[50]}, {node:40, connectedTo:[10,20,50]}, {node:5,connectedTo:[20,30,40] } ])

db.test.aggregate([ {$match: {node:1}} , { $graphLookup: {from : “graph”, startWith: “$node”, connectFromField: “connectedTo”, connectedToField:“node”, as: “demo”} } ]).pretty()
error: MongoServerError: Unknown argument to $graphLookup: connectedToField

Hi @john_Doe3,
The argument name in $graphLookup should be connectToField instead of connectedToField.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

Many thanks for your prompt reply! However, It does not show any output. Am I missing something.

Atlas atlas-odp3nm-shard-0 [primary] sample_training> db.test.aggregate([ {$match: {node:1}} , { $graphLookup: {from : “graph”, startWith: “$node”, connectFromField: “connectedTo”, connectToField:“node”, as: “demo”} } ]).pretty()

It is normal. Your from: collection named graph is empty. You inserted your nodes in a collection named test.

My apology, I have already tried the test without sucess.
db.test.aggregate([ {$match: {node:1}} , { $graphLookup: {from : “test”, startWith: “$node”, connectFromField: “connectedTo”, connectToField:“node”, as: “demo”} } ]).pretty()

There is no node:1 in your test collectio .

my my silly mistake. Many thanks for you patience and help!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.