Mongo:You cannot currently mix including and excluding fields. Contact us if this is an issue.
2013-01-23 12:45
477 查看
在Mongo的Driver中用db.collection.find(<query>,<projection>)方法利用<projection>对象显示指定字段时,报错如上!
起始Mongo的官方文档里解释的是直接设置字段为1,则显示,0就不显示,如下面:
这样结果就会自动显示name字段和age字段,如下所示
[{ "_id" : { "$oid" : "50ff5b28f5e86c4c3281c61c"} , "name" : "abc" , "age" : 13.0}]
在这里要注意一下,无论你要显示的字段是String类型还是int类型,都要填充”“形式,即:query.put("name","");query.put("age","");就可以了,
检索结果会自动出来的!
起始Mongo的官方文档里解释的是直接设置字段为1,则显示,0就不显示,如下面:
DBObject query = new DBObject(); query.put("name",1); query.put("age",1); coll.find(new DBObject("name","lisa"),query);如果按照上面的方式调用,就会报错,如下:
Exception in thread "main" com.mongodb.MongoException: You cannot currently mix including and excluding fields. Contact us if this is an issue. at com.mongodb.MongoException.parse(MongoException.java:82) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295) at com.mongodb.DBCursor._check(DBCursor.java:368) at com.mongodb.DBCursor._hasNext(DBCursor.java:459) at com.mongodb.DBCursor._fill(DBCursor.java:518) at com.mongodb.DBCursor.toArray(DBCursor.java:553) at com.mongodb.DBCursor.toArray(DBCursor.java:542) at com.mongo.test.MongoTest.main(MongoTest.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)经过尝试,我发现,其实只要在query对象中put一个字段,该字段就会显示,对应的,不显式的put一个字段,这个字段就不会显示,如下:
DBObject query = new DBObject(); query.put("name",""); query.put("age",""); coll.find(new DBObject("name","lisa"),query);
这样结果就会自动显示name字段和age字段,如下所示
[{ "_id" : { "$oid" : "50ff5b28f5e86c4c3281c61c"} , "name" : "abc" , "age" : 13.0}]
在这里要注意一下,无论你要显示的字段是String类型还是int类型,都要填充”“形式,即:query.put("name","");query.put("age","");就可以了,
检索结果会自动出来的!
相关文章推荐
- It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing ___Error Installing APK
- Installation failed with message...It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
- This Task Is Currently Locked by a Running Workflow and Cannot Be Edited
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- This task is currently locked by a running workflow and cannot be edited
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- It is possible that this issue is resolved by uninstalling an existing version of the apk if it is
- cocoapods was not able to update the 'master' repo.if it is a unexpected issue and persists you can
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- the android virtual Device AVD2.3.3 is currently running in an emulator and cannot be deleted.
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- The current type is an interface and cannot be constructed. Are you missing a type mapping?
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package
- This task is currently locked by a running workflow and cannot be edited
- This Task Is Currently Locked by a Running Workflow and Cannot Be Edited
- Cannot proceed with delivery: an existing transporter instance is currently uploading this package。