如果存在于mongodb

时间:2017-09-01 07:47:30

标签: mongodb

我想创建一个带来所有文档的查询,但是如果某个特定字段存在则从文档中删除字段并从文档中带出所有其余字段。 例如,如果我在mongodb中有3个文档,我想获取所有文档where aptype = "periodic",如果文档中存在cancel_date字段,则只获取没有cancel_date字段的文档

{  
   "_id":"8de3e5ec-caa9-4497-89a9-239713e2a2ed",
   "user_id":"6b54be23-0880-4e22-8882-93feea935f12",
   "weekend_price":60000,
   "weekend_wage":40000,
   "active":1,
   "dows":[  
      "2",
      "1",
      "6"
   ],
   "product_type_id":"adsfkasjd12312",
   "working_time":320,
   "datetime": ISODate("2017-09-11T10:10:00Z"),
   "price":60000,
   "office_id":"testoffice",
   "wage":40000,
   "manager_id":"653d047a-e9e3-4161-b2b6-7308f9ad38e5",
   "product_id":"543bee5b-43a0-4c98-976f-a371cafb33e4",
   "aptype":"periodic",
   "cancel_date":[  
      ISODate("2017-09-13T00:00:00Z")
   ]
}
{  
   "_id":"5df00497-e95a-4124-abb0-2ade426c741f",
   "product_type_id":"adsfkasjd12312",
   "working_time":320,
   "datetime": ISODate("2017-09-03T10:10:00Z"),
   "wage":100000,
   "aptype":"periodic",
   "dows":[  
      "1",
      "4",
      "6"
   ],
   "price":111111,
   "active":1,
   "weekend_price":70000,
   "weekend_wage":50000,
   "user_id":"c1676bd5-d9b3-452d-8c93-f710176bb33c",
   "office_id":"testoffice",
   "product_id":"140ceaad-1832-444c-95f6-c8053b242bcb",
   "manager_id":""
}
{  
   "_id":"e5d5a046-caf7-4372-a101-f5260ec0af0a",
   "product_type_id":"adsfkasjd12312",
   "working_time":320,
   "datetime": ISODate("2017-09-02T10:10:00Z"),
   "wage":40000,
   "aptype":"one_time",
   "price":60000,
   "active":1,
   "weekend_price":60000,
   "weekend_wage":40000,
   "user_id":"c1676bd5-d9b3-452d-8c93-f710176bb33c",
   "office_id":"testoffice",
   "product_id":"543bee5b-43a0-4c98-976f-a371cafb33e4",
   "manager_id":""
}

所以我想要的结果是(cancel_date字段被删除where _id='8de3e5ec-caa9-4497-89a9-239713e2a2ed'

{  
   "_id":"8de3e5ec-caa9-4497-89a9-239713e2a2ed",
   "user_id":"6b54be23-0880-4e22-8882-93feea935f12",
   "weekend_price":60000,
   "weekend_wage":40000,
   "active":1,
   "dows":[  
      "2",
      "1",
      "6"
   ],
   "product_type_id":"adsfkasjd12312",
   "working_time":320,
   "datetime": ISODate("2017-09-11T10:10:00Z"),
   "price":60000,
   "office_id":"testoffice",
   "wage":40000,
   "manager_id":"653d047a-e9e3-4161-b2b6-7308f9ad38e5",
   "product_id":"543bee5b-43a0-4c98-976f-a371cafb33e4",
   "aptype":"periodic"
}
{  
   "_id":"5df00497-e95a-4124-abb0-2ade426c741f",
   "product_type_id":"adsfkasjd12312",
   "working_time":320,
   "datetime": ISODate("2017-09-03T10:10:00Z"),
   "wage":100000,
   "aptype":"periodic",
   "dows":[  
      "1",
      "4",
      "6"
   ],
   "price":111111,
   "active":1,
   "weekend_price":70000,
   "weekend_wage":50000,
   "user_id":"c1676bd5-d9b3-452d-8c93-f710176bb33c",
   "office_id":"testoffice",
   "product_id":"140ceaad-1832-444c-95f6-c8053b242bcb",
   "manager_id":""
}

1 个答案:

答案 0 :(得分:0)

查看本文档如何消除mongodb中select的字段。 https://docs.mongodb.com/v3.0/tutorial/project-fields-from-query-results/

例如

db.yourtablename.find( { aptype : 'periodic' }, {  cancel_date :0 } )

此查询将删除select语句中的cancel_date。