mongodb使用不同列名的地方

时间:2016-11-29 12:27:37

标签: mongodb mongodb-query where

我在表格中有四条记录,其中一条记录具有不同的列名(“usernm”在下面) -

{ "_id" : ObjectId("583d67ec8a2a25b70f6af482"), "username" : "pradyut", "password" : "hello123" }
{ "_id" : ObjectId("583d68328a2a25b70f6af483"), "username" : "google", "password" : "google123" }
{ "_id" : ObjectId("583d71902a7b4dfc334d1ab4"), "usernm" : "praddy", "password": "google123" }
{ "_id" : ObjectId("583d71b82a7b4dfc334d1ab5"), "username" : "some", "password": "some123" }

以下其中命令无效

db.users.find({$where:"this.username.length>=2"});

并抛出错误 -

 Error: error: {
"ok" : 0,
"errmsg" : "TypeError: this.username is undefined :\n_funcs3@:1:24\n",
"code" : 139
}

在删除“usernm”列时,它可以正常运行

db.users.remove({usernm:"praddy"});

1 个答案:

答案 0 :(得分:2)

检查该字段是否存在并在之后查询,例如

.find({"username" : {$exists:true}, $where :"this.username.length >= 2"})