简体   繁体   English

我试图从mongodb获取单个数组值,但是当我尝试时,我得到了整个对象

[英]I am trying to get a single array value from mongodb, but when i try i get the whole object

1.I Don't get the Item in Electro Array but the whole doc 1.我没有得到电子阵列中的项目,但整个文档

getItem(data){
    dbswap.findOne(
        { 'swap.Items.Electro.id':data.id, 
         'swap.Items.Electro.id':data.id },  function(err,item){
        if(err){
            return (err);
        }
        if(item){     
             console.log(item);                
        }

    });
} // EOF

This is my Schema 这是我的架构
1.I am trying to get the item i create in Electro only, I don't want the whole object i am getting at the moment. 1.我只想获取我在Electro中创建的项目,我现在不想要整个对象。

var swapSchema = new mongoose.Schema({
 swap: {
     name: String,
     Items: {
         Electro: [
             {
                 name: String,
                 info: String,
                 price: Number,
                 dateCreated: Date,
                 category: String,
                 id: Number
              }
          ]
      }
  }
});

Use the projection field : 使用投影场:

If you want to get all the array : 如果要获取所有数组:

   dbswap.findOne(
    { 'swap.Items.Electro.id':data.id},
    { 'swap.Items.Electro' : 1}
   , function(err, obj){

will return something like : 将返回类似:

{
 _id: ObjectId("sdfsdfsdf"),
 Electro:[{....},{....}]
}

Or if you want only the object in the array who match the query : 或者,如果只希望数组中与查询匹配的对象:

   dbswap.findOne(
    { 'swap.Items.Electro.id':data.id},
    { 'swap.Items.Electro.$' : 1}
   , function(err, obj){

will return something like : 将返回类似:

{
 _id: ObjectId("sdfsdfsdf"),
 Electro:{your match object}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将对象推入MongoDB和Nodejs中的数组时,为什么会出现castError? - Why do I get a castError when I try to push an object to an array in MongoDB and Nodejs? 当我尝试将 Object 从 api 添加到我的 mongodb atlas db NODE.JS 时,我变得不确定 - I get undefined when i try to add an Object from an api to my mongodb atlas db NODE JS 当我尝试连接到 mongodb 时出现此错误 - when i try to connect to mongodb get this error 我正在查询 mongoose 模式,但我得到了一堆不必要的东西但需要数组,并且在尝试访问字段时也得到了未定义的值 - I am querying the mongoose schema but i get bunch of unnecessary things but required array and also got undefined value when try to access the fields 当我尝试从 MongoDB 到 postman 获取发布数据时,出现无法设置 header 错误 - When I try to get post data from the MongoDB through postman, I get this cannot set header error 当我尝试连接到 mongodb 时出现此错误 - I get this error when i try to connect to mongodb 尝试从数据库中获取信息时出现错误 - I am getting an error when trying to get information from the database 如何在 MongoDB 中只获取数组元素而不是整个对象作为输出? - How can I get only the array element as output instead of whole object in MongoDB? 我尝试从突变中获取 args 数组。 我仍然得到 [对象:null 原型] - I try get args array from the mutation. And i still get [Object: null prototype] 我怎样才能得到MongoDB这个数组的值呢? - How can I get the value of this array in MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM