zoukankan      html  css  js  c++  java
  • unicloud链表查询

    假设 orders 集合有以下记录:

    [
      {"_id":4,"book":"novel 1","price":30,"quantity":2},
      {"_id":5,"book":"science 1","price":20,"quantity":1},
      {"_id":6}
    ]

    books 集合有以下记录:

    [
      {"_id":"book1","author":"author 1","category":"novel","stock":10,"time":1564456048486,"title":"novel 1"},
      {"_id":"book3","author":"author 3","category":"science","stock":30,"title":"science 1"},
      {"_id":"book4","author":"author 3","category":"science","stock":40,"title":"science 2"},
      {"_id":"book2","author":"author 2","category":"novel","stock":20,"title":"novel 2"},
      {"_id":"book5","author":"author 4","category":"science","stock":50,"title":null},
      {"_id":"book6","author":"author 5","category":"novel","stock":"60"}
    ]

    以下聚合操作可以通过一个相等匹配条件连接 orders 和 books 集合,匹配的字段是 orders 集合的 book 字段和 books 集合的 title 字段:

    const db = uniCloud.database()
    let res = await db.collection('orders').aggregate()
      .lookup({
        from: 'books',
        localField: 'book',
        foreignField: 'title',
        as: 'bookList',
      })
      .end()

    结果:

    [
      {
        "_id": 4,
        "book": "novel 1",
        "price": 30,
        "quantity": 2,
        "bookList": [
          {
            "_id": "book1",
            "title": "novel 1",
            "author": "author 1",
            "category": "novel",
            "stock": 10
          }
        ]
      },
      {
        "_id": 5,
        "book": "science 1",
        "price": 20,
        "quantity": 1,
        "bookList": [
          {
            "_id": "book3",
            "category": "science",
            "title": "science 1",
            "author": "author 3",
            "stock": 30
          }
        ]
      },
      {
        "_id": 6,
        "bookList": [
          {
            "_id": "book5",
            "category": "science",
            "author": "author 4",
            "stock": 50,
            "title": null
          },
          {
            "_id": "book6",
            "author": "author 5",
            "stock": "60",
            "category": "novel"
          }
        ]
      }
    ]
  • 相关阅读:
    盒模型的属性丶display显示丶浮动
    css样式
    HTML(2)
    HTML(1)
    IO多路复用丶基于IO多路复用+socket实现并发请求丶协程
    进程丶数据共享丶锁丶进程池丶模块(爬虫)
    Sublime Text 3 程序运行后中文显示乱码的解决方案
    sublime Text3中文字体错位问题解决办法
    Sublime Text 3 遇到的一些小坑的解决方法
    在Sublime Text 3中配置Python3的开发环境/Build System
  • 原文地址:https://www.cnblogs.com/jyc226/p/14925618.html
Copyright © 2011-2022 走看看