如何为以下描述编写 mongodb 聚合查询

Posted

技术标签:

【中文标题】如何为以下描述编写 mongodb 聚合查询【英文标题】:how to write a mongodb aggregation query for following description 【发布时间】:2018-07-20 09:48:29 【问题描述】:

我需要查询: 其用户 ID 为:“userId”:“111b/c”,“userId”:“111a”。根据用户 ID,他们有多少 $eq:light_pad:20 我需要 userId:111b/c-max date,userId:111a-max date

数据库数据

 "_id" : ObjectId("5a7c1d167826eaca4b4e4398"), "firstName" : "varun", "secName" : "raju", "userId" : "111a", "light_pad" : "20", "lights" : "on", "CREATE_DATE" : ISODate("2018-05-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "76%", "Address" :  "Street" : "Burkit Road", "City" : "chennai", "State" : "Tamil Nadu" , "Gender" : "Male"  
 "_id" : ObjectId("5a7c1db77826eaca4b4e4399"), "firstName" : "raju", "secName" : "rani", "userId" : "111a", "light_pad" : "40", "lights" : "off", "CREATE_DATE" : ISODate("2017-04-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "86%", "Address" :  "Street" : "Burkit Road", "City" : "chennai", "State" : "Tamil Nadu" , "Gender" : "female"  
 "_id" : ObjectId("5a7c1e707826eaca4b4e439b"), "firstName" : "ranga", "secName" : "mahesh", "userId" : "111a", "light_pad" : "26", "lights" : "on", "CREATE_DATE" : ISODate("2012-04-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "7%", "Address" :  "Street" : "Burkit ", "City" : "vizag", "State" : "Tamil Nadu" , "Gender" : "female"  
 "_id" : ObjectId("5a7c1f5d7826eaca4b4e439d"), "firstName" : "kalu", "secName" : "ramana", "userId" : "111b/c", "light_pad" : "25", "lights" : "off", "CREATE_DATE" : ISODate("2014-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "46%", "Address" :  "Street" : "Burkit ", "City" : "bhimavaram", "State" : "Tamil Nadu" , "Gender" : "male"  
 "_id" : ObjectId("5a7c1fb37826eaca4b4e439e"), "firstName" : "praveen", "secName" : "pani", "userId" : "111b/c", "light_pad" : "52", "lights" : "on", "CREATE_DATE" : ISODate("2016-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "36%", "Address" :  "Street" : "vin ", "City" : "palakoderu", "State" : "Tamil Nadu" , "Gender" : "male" 
 "_id" : ObjectId("5a7c20057826eaca4b4e439f"), "firstName" : "vani", "secName" : "karun", "userId" : "111b/c", "light_pad" : "52", "lights" : "on", "CREATE_DATE" : ISODate("2013-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "76%", "Address" :  "Street" : "bali ", "City" : "srikakulam", "State" : "Tamil Nadu" , "Gender" : "male"  

任何答案将不胜感激 提前致谢。

【问题讨论】:

【参考方案1】:

我没有正确理解您的问题,但我有一个可能与您的问题相关的解决方案。希望对你有帮助。

db.test.aggregate([
    $match: 
        _id: 
            $in: [ObjectId("5a7c1d167826eaca4b4e4398"), 
ObjectId("5a7c1db77826eaca4b4e4399")]
        ,
        light_pad: 
            $eq: "20"
        
    
]);

它会给出给定的结果:


"_id" : ObjectId("5a7c1d167826eaca4b4e4398"),
"firstName" : "varun",
"secName" : "raju",
"userId" : "111a",
"light_pad" : "20",
"lights" : "on",
"CREATE_DATE" : ISODate("2018-05-08T03:12:00.000Z"),
"Department" : "Computer Science and Engg",
"Percentage" : "76%",
"Address" : 
    "Street" : "Burkit Road",
    "City" : "chennai",
    "State" : "Tamil Nadu"
,
"Gender" : "Male"

【讨论】:

以上是关于如何为以下描述编写 mongodb 聚合查询的主要内容,如果未能解决你的问题,请参考以下文章

如何为以下语句编写标准构建器 API JPA 查询

如何为嵌入文件编写mongo查询

如何为以下查询编写QueryExpression?

如何为每一行编写加入日期时间的 SQL 查询

如何在 Symfony 中为使用 queryBuilder 构建的 MongoDB 查询编写单元测试

MongoDB GeoNear 聚合