我在使用 CakePHP 的 MongoDB 方面比较新.
I am newer in MongoDB with CakePHP.
当我编写以下查询时,它会执行得很好.
When I write the following query it will execute very well.
db.testData.find() { "_id" : ObjectId("53d1f79db8173a625961ff3d"), "name" : "sadikhasan", "created" : ISODate("2014-07-25T06:22:21.701Z") }当我运行以下查询以仅获取 name 时,它返回一个错误:
When I run the following query to get only name, it returns an error:
db.testData.find({},{name:1, created:0}) error: { "$err" : "Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.", "code" : 17287 }当我运行以下查询以仅获取带有 _id:0 的 name 时,它执行得很好:
When I run the following query to get only name with _id:0, then it executes well:
db.testData.find({},{name:1, _id:0}) { "name" : "sadikhasan" }我的问题是,为什么在投影列表中写入 created:0 时会出现错误.提前感谢您的帮助.
My question is, why I am getting an error when I write created:0 in the projection list. Thanks for help in advance.
推荐答案不能混合包含和排除,唯一的例外是 _id 字段.
You cannot mix inclusion and exclusion, the only exception is the _id field.
例如,如果你有这个:
{ "_id": ObjectId("53d1fd30bdcf7d52c0d217de"), "name": "bill", "birthdate": ISODate("2014-07-80T00:00:00.000Z"), "created": ISODate("2014-07-25T06:44:38.641Z") }如果您只需要姓名"和生日",您需要这样做:
If all you want is the "name" and "birthdate" you need to do this:
db.collection.find({},{ "_id": 0, "name": 1, "birthdate": 1 })或者这个:
db.collection.find({},{ "_id": 0, "created": 0 })但不允许混合"除_id"之外的任何其他操作
But it is not allowed to "mix" any other operations other than "_id"
db.collection.find({},{ "_id": 0, "name": 1, "created": 0 })这也会产生错误.
手册页.
更多推荐
MongoDB:无法规范化查询:BadValue Projection 不能混合包含和排除
发布评论