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

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

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

问题

这是样本文档:

  1. {
  2. _id: ObjectId("60a8f82df06d8601849b2a01"),
  3. pokemon: [
  4. {
  5. "id": 1,
  6. "num": 1,
  7. "name": "bulbasaur",
  8. "img": "img.com",
  9. "type": ["grass", "poison"],
  10. "height": 112,
  11. "weight": 33
  12. },
  13. {
  14. "id": 2,
  15. "num": 2,
  16. "name": "char",
  17. "img": "img.com",
  18. "type": ["grass", "poison"],
  19. "height": 112,
  20. "weight": 33
  21. }
  22. ]
  23. }

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

这是我的代码:

  1. const client = new MongoClient(url);
  2. console.log("connected")
  3. await client.connect();
  4. const db = client.db('cluster0');
  5. const collection = db.collection('testing');
  6. let datas = await collection.find({
  7. 'pokemon.0.name': "bulbasaur"
  8. }).toArray();
  9. console.log(datas)

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

英文:

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

  1. {
  2. _id: ObjectId("60a8f82df06d8601849b2a01")
  3. pokemon: [
  4. {
  5. "id": 1,
  6. "num": 1,
  7. "name": "bulbasaur",
  8. "img": "img.com",
  9. "type": ["grass", "poison"],
  10. "height": 112,
  11. "weight": 33
  12. },
  13. {
  14. "id": 2,
  15. "num": 2,
  16. "name": "char",
  17. "img": "img.com",
  18. "type": ["grass", "poison"],
  19. "height": 112,
  20. "weight": 33
  21. }
  22. ]
  23. }

I want to extract the names from this list.

This is my code

  1. const client = new MongoClient(url);
  2. console.log("connected")
  3. await client.connect();
  4. const db = client.db('cluster0');
  5. const collection = db.collection('testing');
  6. let datas = await collection.find({
  7. 'pokemon.0': {
  8. name: "bulbasaur"
  9. }
  10. }).toArray();
  11. console.log(datas)

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

答案1

得分: 1

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

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

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

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

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

英文:

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

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

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

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

See how it works on the playground example

答案2

得分: 1

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

  1. db.collection.find({
  2. "pokemon.name": "Bulbasaur"
  3. },
  4. {
  5. "pokemon.$": 1
  6. })
英文:

If you want particularly that object only then use this

  1. db.collection.find({
  2. "pokemon.name": "Bulbasaur"
  3. },
  4. {
  5. "pokemon.$": 1
  6. })

答案3

得分: 0

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

  1. db.collection.find({
  2. "pokemon": {
  3. "$elemMatch": {
  4. name: "bulbasaur"
  5. }
  6. }
  7. })

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

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

英文:

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

  1. db.collection.find({
  2. "pokemon": {
  3. "$elemMatch": {
  4. name: "bulbasaur"
  5. }
  6. }
  7. })

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:

确定