Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

MongoDB sorting by documents with more fields filled out

I'm currently trying to get a query that I can find by zip code (very easy)

e.g. db.col.find({zip: 60010})

but then sort based on how many fields on the Mongo document are filled out (or not null; not so easy)

How can I do this in a fast an efficient way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do it using objectToArday and addFields then size

You can refer this answer

But it's costlier option than having one field which tells the count of fields in that document.

You can decide based on read or write intensive application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...