在MongoDB C#中获取有关同一集合的其他数据

发布于 2025-01-19 14:55:52 字数 682 浏览 1 评论 0原文

我有一个名为“用户”的集合。我正在通过用户ID获取记录。除此之外,我还需要其他10个最后10个更新(DateTime)记录不包括USERID记录,但加在一起。因此,在这种情况下,总返回结果将为11。是否可以使用同一查询?我尝试使用或查找,但无法按预期工作。 任何帮助都将受到赞赏。

 User collection:
[{
     "id" : "123456", 
    "name" : "foo", 
    "addressIds" : [ObjectId(234567)]
  }     ,
    "id" : "345678", 
    "name" : "bar", 
    "addressIds" : [ObjectId(678565), ObjectId(567456)]
   }]

 Address collection:
 [{
 "_id":"234567",
 "district" : "district1", 
 "pincode" : "568923",
  },
  {
 "_id":"678565",
 "district" : "district2", 
 "pincode" : "568924",
  },
  {
 "_id":"567456",
 "district" : "district3", 
 "pincode" : "568925",
 }]

使用方面,我有用户和地址。我可以在用户中有用于地址的实际文档吗?

I have a collection called "User". I'm passing userid to get the record. In addition to that i also need additional 10 last updatedAt(DateTime) record excluding the userid record but added together. So, total returned result will be 11 in this case. Is that possible using same query? I tried using Or and lookup but can't make it work as expected.
Any help is appreciated.

 User collection:
[{
     "id" : "123456", 
    "name" : "foo", 
    "addressIds" : [ObjectId(234567)]
  }     ,
    "id" : "345678", 
    "name" : "bar", 
    "addressIds" : [ObjectId(678565), ObjectId(567456)]
   }]

 Address collection:
 [{
 "_id":"234567",
 "district" : "district1", 
 "pincode" : "568923",
  },
  {
 "_id":"678565",
 "district" : "district2", 
 "pincode" : "568924",
  },
  {
 "_id":"567456",
 "district" : "district3", 
 "pincode" : "568925",
 }]

Using facets, i have the User and the addressIds. Can i have the actual documents for AddressIds in User?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2025-01-26 14:55:53

编辑:
您可以使用$ facet,如下:

db.collection.aggregate([
  {$sort: {date: -1}},
  {$facet: {
      byTate: [{$limit: 10}],
      byUser: [{$match: {userId: 455845}}]
    }
  },
  {$project: {
    byDate: {
        $filter: {
            input: "$byDate",
            as: "item",
            cond: {$ne: ["$item",{"$arrayElemAt": ["$byUser", 0]}]}
        }
    },
    byUser: 1,
    }
  },
  {
    $project: {
      byDate: {$slice: ["$byDate", 10]},
      byUser: 1
    }
  }
])

您可以看到它在

在用户ID之间切换:455845 /455845以查看两种情况。

Edit:
You can use $facet, like this:

db.collection.aggregate([
  {$sort: {date: -1}},
  {$facet: {
      byTate: [{$limit: 10}],
      byUser: [{$match: {userId: 455845}}]
    }
  },
  {$project: {
    byDate: {
        $filter: {
            input: "$byDate",
            as: "item",
            cond: {$ne: ["$item",{"$arrayElemAt": ["$byUser", 0]}]}
        }
    },
    byUser: 1,
    }
  },
  {
    $project: {
      byDate: {$slice: ["$byDate", 10]},
      byUser: 1
    }
  }
])

You can see it works on the playground .

Switch between userId: 455845 / 455845 to see both cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文