Maison  >  Questions et réponses  >  le corps du texte

mongodb 针对某列值返回list

mongodb中数据结构为
{'_id': 1, 'name': 'a'}
{'_id': 2, 'name': 'b'}
{'_id': 3, 'name': 'c'}

现在可以通过
db.collection.find({}, {'name': 1})
得到list为:
[
    {'name': 'a'},
    {'name': 'b'},
    {'name': 'c'}
]

有什么方法可以得到下面的值吗(不希望把数据load到内存再循环list进行处理,mongodb本身有没有提供什么方法)
['a', 'b', 'c']
迷茫迷茫2727 Il y a quelques jours569

répondre à tous(1)je répondrai

  • PHP中文网

    PHP中文网2017-05-02 09:20:50

    mongodb prend en charge la syntaxe js, alors utilisez simplement map directement

    // es6 
    db.collection.find({}, {'name': 1}).map( x => x.name ) // [ "a", "b", "c" ]
    
    db.collection.find({}, {'name': 1}).map(function(x) {return x.name} ) 

    Pour l'introduction de la carte, veuillez consulter https://danmartensen.svbtle.com/javascripts-map-reduce-and-filter#map_1

    répondre
    0
  • Annulerrépondre