1

I'm doing a project with Mongoose in Node.js. I have documents with this format:

{
  username:"user1",
  name:"joun",
  books:[4,12,34,5,13]
}

I use find method for fetching them. it works but i have a problem. I want that get length of books field rather than it's data. I did a lot's of research on web and find out Mongodb and Mongoose has some limitations in comparison of relation databases(there is no Join's). In the end i find two methods to do this but both of the i think are very slow and not optimum.

1- after fetching results, in a forEach loop, calculate length of books filed and replace that value with main value

2- after fetching results, using mongodb Aggregation framework i have to do some work to calculate that. but it seem this method is very expensive.

My server will be in heavy load and i think this two method's can be bottleneck for my architecture.

There is not any other option for me? i need faster and standard way to do it. some people say's you have to use a relational database like postgres but i cant. This is question for me that in lack of some functionalities like joins in mongodb, can we do every thing in proper way? my problem is an example!

Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • Possible dupe of http://stackoverflow.com/questions/6722850/querying-internal-array-size-in-mongodb – JohnnyHK Jul 28 '15 at 18:12
  • @JohnnyHK: it's not dupe, my question is about another way (if exist). this post explains Aggregation that i explained. – Fcoder Jul 28 '15 at 18:27
  • I think you may want to try about Indexes, Mongodb Indexes are special type of data and they efficient way to query essential data. – greenlikeorange Jul 28 '15 at 18:39
  • @Fcoder Ok, it wasn't clear to me from your question that that's what you meant. I would suggest trying aggregation first instead of just assuming its performance won't be adequate. – JohnnyHK Jul 28 '15 at 19:00
  • This seems to be one of those questions where both the title and the current content are misleading to your actual intent. Rather than ask "how to get the current length" ( for which the only answer if you don't store a length field is "use aggregate" ), or rant about dislikes of mongoose or MongoDB, how about actually "explaining" your update process and current code. The "dot points" here do not do it for me or anyone else, and neither does "I need a faster way". Show you current coding process and what you are trying to achieve from it. – Blakes Seven Jul 28 '15 at 23:30

0 Answers0