【问题标题】:Getting different values in mongoose & mongoDB despite of using same syntax尽管使用相同的语法,但在 mongoose 和 mongoDB 中获取不同的值
【发布时间】:2017-11-20 10:04:39
【问题描述】:

我在 mongoose 和 mongo shell 中使用相同的语法,但两者都返回不同的值

这是我的主要数据

      "username" : "developer2",
    "createdProjects" : [ ],
    "registeredClasses" : [ ],
    "friendRequest" : [
            {
                    "id" : ObjectId("5a114dede58f4a05cb135d96"),
                    "username" : "chirag",
                    "_id" : ObjectId("5a114dfee58f4a05cb135d97"),
                    "Color" : "green",
                    "Status" : "Friend"
            }
    ],
    "messages" : [
            {
                    "id" : ObjectId("5a109488b5454805a94f74ab"),
                    "User" : "Sting",
                    "_id" : ObjectId("5a114d51e58f4a05cb135d91"),
                    "texts" : [
                            {
                              "username" : "Sting",
                              "message" : "message",
                             "_id"ObjectId("5a114d51e58f4a05cb135d93")
                            },
                            {
                                    "username" : "developer2",
                                    "message" : "developer 2",
                             "_id"ObjectId("5a114dbbe58f4a05cb135d94")
                            }
                    ]
            },
            {
                    "id" : ObjectId("5a114dede58f4a05cb135d96"),
                    "User" : "chirag",
                    "_id" : ObjectId("5a114e26e58f4a05cb135d9a"),
                    "texts" : [
                            {
                             "username" : "developer2",
                             "message" : "hi chira",
                             "_id"ObjectId("5a114e26e58f4a05cb135d9c")
                            },
                            {
                                    "username" : "chirag",
                                    "message" : "hi developer",
                             "_id"ObjectId("5a114e40e58f4a05cb135d9f")
                            }
                    ]
            }
    ],
    "friends" : [
            {
                    "username" : "Sting",
                    "_id" : ObjectId("5a114d38e58f4a05cb135d8f")
            },
            {
                    "username" : "chirag",
                    "_id" : ObjectId("5a114e16e58f4a05cb135d98")
            }
    ]

这是我的 mongo 代码[运行良好]

 db.users.find({username:"chirag"},
  {messages: 
   { $elemMatch: 
    { User: 
      "developer2" } }})

返回以下 json 数据

   {
    "_id" : ObjectId("5a114dede58f4a05cb135d96"),
    "messages" : [
            {
                    "id" : ObjectId("5a114c66e58f4a05cb135d8c"),
                    "User" : "developer2",
                    "_id" : ObjectId("5a114e26e58f4a05cb135d9b"),
                    "texts" : [
                            {
                             "username" : "developer2",
                             "message" : "hi chira",
                            "_id":ObjectId("5a114e26e58f4a05cb135d9d")
                            },
                            {
                              "username" : "chirag",
                              "message" : "hi developer",
                            "_id":ObjectId("5a114e40e58f4a05cb135d9e")
                            }
                    ]
            }
    ]
}

这正是我想要的 ,但是当我在猫鼬中使用相同的方法时,它返回的值不同

   User.find({username:req.user.username},{messages:
{ $elemMatch: { 
  User:req.params.username} }},function(err,data){
    if (err) {
      throw err;
   } else {
    res.render("chat",{texts:data,user:founduser});
   }
  }) 

在数据中返回这个值

  { _id: 5a114dede58f4a05cb135d96,
     createdProjects: [], 
    registeredClasses: [],
    friendRequest: [],
    messages: [ { id: 5a114c66e58f4a05cb135d8c,
              User: 'developer2',
             _id: 5a114e26e58f4a05cb135d9b,
              texts: [Array] } ],
    friends: [] }

我遇到的第一个问题是返回正确的数据以及我什至没有要求的数据 还有以 [Array] 形式出现的文本,我如何从中提取信息 请说明我在这里缺少的内容

【问题讨论】:

    标签: json node.js mongodb mongoose


    【解决方案1】:

    默认情况下,Mongoose 会为您提供整个匹配的文档, 如果您想选择您想要的信息,请使用projection

    示例:

    // find each person with a last name matching 'Ghost',
    //  selecting the `name` and `occupation` fields
    Person.findOne({
      'name.last': 'Ghost',
    }, 'name occupation', function (err, person) {
      ...
    })
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2012-10-31
      • 2021-08-13
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2023-03-26
      相关资源
      最近更新 更多