MongoDB:无法规范化查询:BadValue投影不能混合包含和排除 [英] MongoDB: Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion

查看:643
本文介绍了MongoDB:无法规范化查询:BadValue投影不能混合包含和排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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
}

当我运行以下查询以仅获取name_id:0时,它执行得很好:

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.

例如,如果您有以下内容:

For example if you have this:

{
   "_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投影不能混合包含和排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆