when try to pull an object from array in mongodb acknowledged: true, modifiedCount: 0, upsertedId: null, upsertedCount: 0, matchedCount: 1

huangapple go评论71阅读模式
英文:

when try to pull an object from array in mongodb acknowledged: true, modifiedCount: 0, upsertedId: null, upsertedCount: 0, matchedCount: 1

问题

I'm trying to remove a product object from the user's cart when its count reaches zero.

changeProductCount: (details) => {
  return new Promise(async (resolve, reject) => {
    try {
      if (details.count == -1 && details.quantity == 1) {
        console.log();
        let response = await db.get().collection(CART_COLLECTION)
          .updateOne({
            $and: [
              { _id: ObjectId(details.cart) },
              { 'products.time': parseInt(details.time) }
            ]
          }, {
            $pull: {
              products: { item: ObjectId(details.item) }
            }
          });

        if (response) {
          console.log(response);
          resolve({ removeProduct: true })
        }
      } else {
        let response = await db.get().collection(CART_COLLECTION)
          .updateOne({
            _id: ObjectId(details.cart),
            'products.time': details.time
          }, {
            $inc: {
              'products.$.quantity': parseInt(details.count)
            }
          });

        if (response) {
          console.log(response);
          resolve({ removeProduct: false })
        }
      }
    } catch (error) {
      reject(error)
    }
  })
}

This is my code. I'm trying to pull an object from an array in the userCart when the product count is 0.

Here, if I replace the code as follows:

let response = await db.get().collection(CART_COLLECTION)
  .updateOne({
    _id: ObjectId(details.cart)
  }, {
    $pull: {
      products: {
        item: ObjectId(details.item)
      }
    }
  }
);

This code is working, but the problem is that if there are two shirts with the same product ID but different sizes, say Medium and Large, when Medium is removed, Large also gets removed. That's the reason why I added a time for each object when it is first added to the cart. However, it is not working. Please help me with this problem.

This is the response that I get:

{
  acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}
英文:

I'm trying to remove a product object from user cart when it's count reaches zero.

changeProductCount : (details) => {
  return new Promise(async (resolve, reject) => {
    try {
      if (details.count==-1 && details.quantity==1) {
        console.log();
        let response = await db.get().collection(CART_COLLECTION)
          .updateOne({
            $and: [
              { _id: ObjectId(details.cart) }, 
              { 'products.time': parseInt(details.time) }
            ]
          }, {
            $pull : {
              products : { item : ObjectId(details.item) }
            }
          });

        if (response) {
          console.log(response);
          resolve({ removeProduct: true })
        }
      } else {
        let response = await db.get().collection(CART_COLLECTION)
          .updateOne({
            _id: ObjectId(details.cart),
            'products.time': details.time
          }, {
            $inc : {
              'products.$.quantity': parseInt(details.count)
            }
          });

        if (response) {
          console.log(response);
          resolve({removeProduct:false})
        }
      } 
    } catch (error) {
      reject(error)
    }
  })
}

This is my code. I'm trying to pull an object from an array from the userCart when their product count is 0.

Here, if I replace the code as this:

let response = await db.get().collection(CART_COLLECTION)
  .updateOne({
    _id:ObjectId(details.cart)
  }, {
    $pull: {
      products: {
        item : ObjectId(details.item)
      }
    }
  }
);

This code is working, but the problem is, if there are two shirts with same product Id but different sizes, say Medium and Large, when medium is removed, large also gets removed. That's the reason why I added time for each objects when it is first added to cart. But, it is not working. Please help me in this problem.

This is the response that I get:

{
  acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

答案1

得分: 0

我解决了这个问题。这是我使用的代码。

let response = await db.get().collection(CART_COLLECTION).updateOne({_id:ObjectId(details.cart)},{$pull : {products : {time : parseInt(details.time)}}});

由于时间已经是唯一值,我使用它来与数组对象匹配。

英文:

I solved this problem. This is the code that I used.

let response = await db.get().collection(CART_COLLECTION).updateOne({_id:ObjectId(details.cart)},{$pull : {products : {time : parseInt(details.time)}}});

As the time was already a unique value, I used it to match with the array object.

huangapple
  • 本文由 发表于 2023年1月5日 18:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75017235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定