如何从嵌套对象列表中投影特定字段的列表?

huangapple go评论86阅读模式
英文:

How to project a list of a specific field from a nested list of objects?

问题

这是样本文档:

{
  _id: ObjectId("60a8f82df06d8601849b2a01"),
  pokemon: [
    {
      "id": 1,
      "num": 1,
      "name": "bulbasaur",
      "img": "img.com",
      "type": ["grass", "poison"],
      "height": 112,
      "weight": 33
    },
    {
      "id": 2,
      "num": 2,
      "name": "char",
      "img": "img.com",
      "type": ["grass", "poison"],
      "height": 112,
      "weight": 33
    }
  ]
}

我想要从这个列表中提取名称。

这是我的代码:

const client = new MongoClient(url);
console.log("connected")
await client.connect();
const db = client.db('cluster0');
const collection = db.collection('testing');

let datas = await collection.find({
    'pokemon.0.name': "bulbasaur"
}).toArray();
console.log(datas)

尝试了不同的查询操作符,如文本匹配,但仍然不起作用。

英文:

I have mongodb documents, each contains a pokemon array, where each item is an object. This is a sample document:

{
  _id: ObjectId("60a8f82df06d8601849b2a01")
  pokemon: [
    {
      "id": 1,
      "num": 1,
      "name": "bulbasaur",
      "img": "img.com",
      "type": ["grass", "poison"],
      "height": 112,
      "weight": 33
    },
    {
     
      "id": 2,
      "num": 2,
      "name": "char",
      "img": "img.com",
      "type": ["grass", "poison"],
      "height": 112,
      "weight": 33
    }
  ]
}

I want to extract the names from this list.

This is my code

const client = new MongoClient(url);
console.log("connected")
await client.connect();
const db = client.db('cluster0');
const collection = db.collection('testing');

let datas = await collection.find({
    'pokemon.0': {
        name: "bulbasaur"
    }
}).toArray();
console.log(datas)

Tried different query operators such as text matching but still not working

答案1

得分: 1

如果你想查找一个其第一个宝可梦的名字是"bulbasaur"的文档,请尝试:

let data = await collection.find({"pokemon.0.name": "bulbasaur"})

如果你只想返回宝可梦的名字,你可以使用投影:

let data = await collection.findOne(
  {"pokemon.0.name": "bulbasaur"},
  {_id: 0, names: {$map: {input: "$pokemon", in: "$$this.name"}}}
)
console.log(data.names)

示例播放器中查看其运行方式

英文:

If you want to find a document which its first pokemon's name is "bulbasaur", try:

let data = await collection.find({"pokemon.0.name": "bulbasaur"})

And if you want to return only the pokemon names, you can use projection:

let data = await collection.findOne(
  {"pokemon.0.name": "bulbasaur"},
  {_id: 0, names: {$map: {input: "$pokemon", in: "$$this.name"}}}
)
console.log(data.names)

See how it works on the playground example

答案2

得分: 1

如果你特别想要只获取那个对象,那么使用这个:

db.collection.find({
  "pokemon.name": "Bulbasaur"
},
{
  "pokemon.$": 1
})
英文:

If you want particularly that object only then use this

db.collection.find({
  "pokemon.name": "Bulbasaur"
},
{
  "pokemon.$": 1
})

答案3

得分: 0

如果您想使位置动态,并仍然搜索“bulbasaur”,您可以使用:

db.collection.find({
  "pokemon": {
    "$elemMatch": {
      name: "bulbasaur"
    }
  }
})

您可以在此处阅读更多信息 => 查询嵌入式文档数组

您还可以在 playground 上进行测试。

英文:

If you want position to be dynamic and still search for "bulbasaur" you can use:

db.collection.find({
  "pokemon": {
    "$elemMatch": {
      name: "bulbasaur"
    }
  }
})

You can read more about it here =>
Query an Array of Embedded Documents

You can also test it on playground

huangapple
  • 本文由 发表于 2023年7月17日 19:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定