【问题标题】:How to get data that is the same type, but not including the one given in MongoDB如何获取相同类型的数据,但不包括 MongoDB 中给出的数据
【发布时间】:2022-01-10 15:48:17
【问题描述】:

我正在尝试获取与给定产品相似的产品列表(与给定产品类型相似),但不包括给定产品。

我的数据库中有 3 个床位实例,因此我想获取它们的其他 2 个实例,但不返回原始 1,在本例中其 ID 为 1。

查看文档后,我认为以下内容可行,但它给了我一个 500 内部错误。

product_type = "Bed"
product_id = 1

client = MongoClient("...")
db = client.assignment

theId = db.products.find({"_id": {"$ne": product_id}})
theProd = db.products.find({"product_type": {"$eq": product_type}})

myCursor = db.products.find({"$and": [theId, theProd]})
list_cur = list(myCursor)
json_data = dumps(list_cur)
return json_data

【问题讨论】:

    标签: python mongodb google-cloud-functions


    【解决方案1】:

    我发现了这个问题,对于以后看到这个的人,我改变了

    theId = db.products.find({"_id": {"$ne": product_id}})
    theProd = db.products.find({"product_type": {"$eq": product_type}})
    
    myCursor = db.products.find({"$and": [theId, theProd]})
    

    myCursor = db.products.find({"product_type": {"$eq": product_type}, "_id": {"$ne": product_id}})
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 2014-10-25
      • 2019-09-06
      • 1970-01-01
      • 2016-10-31
      • 2023-03-29
      • 2016-10-18
      • 2019-09-16
      • 1970-01-01
      相关资源
      最近更新 更多