Mongo DB $ push对象首先将其包装在一个数组中

时间:2017-01-17 08:48:19

标签: javascript mongodb

当我在mongodb中使用$ push时,预期结果会有所不同。它实际上包装了我想要在数组中推送的对象。这是有问题的,因为我必须映射结果才能提取它。任何帮助将不胜感激。

我的Mongo查询

const pushAction = {
    $push: {
      cart: {
        id: itemId,
        quantity: quantity
      }
    }
  }

  // Add item to user's cart
  User.update({_id: userId}, pushAction, (err, success) => {
    if (err) {
      res.status(422).json({'error': 'There was a problem adding the item to your cart.'});
    }
    if (success) {
      // Find user and return the cart
      User.findOne({_id: userId}, {cart: 1}, (err, user) => {
        res.status(200).json({'message': 'The item was successfully added to your cart.', cart: user.cart});
      })
    }
  });

用户架构

// Define User Model
const userSchema = new Schema({
  firstName: {
    type: Schema.Types.String,
    required: true
  },
  lastName: {
    type: Schema.Types.String,
    required: true
  },
  password: {
    type: Schema.Types.String,
    required: true
  },
  email: {
    type: Schema.Types.String,
    required: true
  },
  cart: {
    type: Schema.Types.Array
  },
  dateCreated: {
    type: Schema.Types.Date,
    default: Date.now,
    required: true
  },
  dateUpdated: [
    {
      date: {
        type: Schema.Types.Date
      },
      details: {
        type: Schema.Types.ObjectId
      }
    }
  ],
  verified: {
    type: Schema.Types.Boolean,
    required: true
  },
  role: {
    type: Schema.Types.String,
    default: ROLES_BASIC_USER
  }
});

预期结果

"cart" : [ 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }, 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }, 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }
],

实际结果

"cart" : [ 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ], 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ], 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ]
    ]

2 个答案:

答案 0 :(得分:4)

// Define User Model
const userSchema = new Schema({
  firstName: {
    type: Schema.Types.String,
    required: true
  },
  lastName: {
    type: Schema.Types.String,
    required: true
  },
  password: {
    type: Schema.Types.String,
    required: true
  },
  email: {
    type: Schema.Types.String,
    required: true
  },
  cart:[ {
    id: Schema.Types.ObjectId,
    quantity: Number 
  }],
  dateCreated: {
    type: Schema.Types.Date,
    default: Date.now,
    required: true
  },
  dateUpdated: [
    {
      date: {
        type: Schema.Types.Date
      },
      details: {
        type: Schema.Types.ObjectId
      }
    }
  ],
  verified: {
    type: Schema.Types.Boolean,
    required: true
  },
  role: {
    type: Schema.Types.String,
    default: ROLES_BASIC_USER
  }
});

答案 1 :(得分:1)

尝试更改pushAction,如下所示:

const pushAction = {
    $push: {
        cart: { $each: [ {id: itemId, quantity: quantity } ] }
    }
}

在尝试此操作之前清除cart字段中的现有项目。 如果它仍然失败,则问题可能出在架构上。