【问题标题】:how to write a mongodb aggregation query for following description如何为以下描述编写 mongodb 聚合查询
【发布时间】: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" } 

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

【问题讨论】:

    标签: node.js mongodb mongodb-query aggregation-framework aggregate-functions


    【解决方案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"
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多