最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return only value of a field in mongodb - Stack Overflow

programmeradmin2浏览0评论

After applying the find operation in mongodb.. i get the following list of documents..

  db.users.find(....)

i got:

 { "text" : "Hey" }
 { "text" : "Hi" }
 { "text" : "Hello" }
 { "text" : "yes" }

How can i convert it into

 ["Hey","Hi","Hello","yes"].

i tried

 db.users.find(...).map( function(u) { return "u.text"; } )

but it is giving error!

After applying the find operation in mongodb.. i get the following list of documents..

  db.users.find(....)

i got:

 { "text" : "Hey" }
 { "text" : "Hi" }
 { "text" : "Hello" }
 { "text" : "yes" }

How can i convert it into

 ["Hey","Hi","Hello","yes"].

i tried

 db.users.find(...).map( function(u) { return "u.text"; } )

but it is giving error!

Share Improve this question edited Sep 22, 2017 at 18:01 CommunityBot 11 silver badge asked May 15, 2014 at 10:54 shashankshashank 9772 gold badges7 silver badges8 bronze badges 10
  • And in SQL ( as opposed to "noSQL" ) you do that how? This actually is way to broad a question to be constructive to anyone here as well as being impossible without post processing in any environment. – Neil Lunn Commented May 15, 2014 at 11:00
  • sorry, but i didn't get you.. what are you trying to say – shashank Commented May 15, 2014 at 11:03
  • i tried db.users.find(...).map( function(u) { return "u.text"; } ) but it is giving error! – shashank Commented May 15, 2014 at 11:05
  • And usage such as map as you described above is called "post processing". Also you need to learn how to edit your question here rather than supply relevant information in comments. – Neil Lunn Commented May 15, 2014 at 11:09
  • so why this command is giving error? and whats the correct method to di it? – shashank Commented May 15, 2014 at 11:14
 |  Show 5 more comments

6 Answers 6

Reset to default 17

Not sure what you language implementation is but the basic concept is:

var result = []
db.users.find().forEach(function(u) { result.push(u.text) })

And the returned value to result is:

["Hey","Hi","Hello","yes"]

At first db.users.find(...).map() didn't work because db.users.find(...) doesn't return you a real array.

So you need to convert to array at first.

db.users.find(...).toArray()

Then if you apply map() function will work

  db.users.find(...).toArray().map( function(u) { return u.text ; } )

Another simple trick is using .forEach()

This will do the trick

var cursor = db.users.find(...); // returns cursor object which is a pointer to result set

var results = [];
cursor.forEach(
  function(row) {
     results.push(row.text);
  });

results //results will contain the values

Another option is simply to use distinct:

db.users.distinct("first_name");

Would return:

[
  "John",
  "Jennifer",
  ...
]

you can use

var u=db.users.find({...},{text:1,_id:0})
while(u.hasNext()){print(u.Next().text);}

The correct answer here is the method .distinct() (docs)

In your case try it like this:

db.users.find(....).distinct('text')

That will return only the values.

best way is :

db.users.distinct("text");


["Hey","Hi","Hello","yes"].

You will get further information regargding this topic here : mongodb distinct

发布评论

评论列表(0)

  1. 暂无评论