MongoDB查询不存在字段将导致全表扫描

1.我们知道当查询mysql 表时,若字段不存在,将导致报错。

mysql> select `field_a` from `user`;
ERROR 1054 (42S22): Unknown column 'field_a' in 'field list'

2.但是查询mongodb collection时,若字段不存在将怎样?

注意到了吗?!“stage” : “COLLSCAN” ! ! 千万小心。全表扫描后果很严重。

> db.user.find({field_a:"test"}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "sdk_admin.user",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"field_a" : {
				"$eq" : "test"
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"field_a" : {
					"$eq" : "test"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "sdk-mongo-db-778cf75659-g79zp",
		"port" : 27017,
		"version" : "3.4.13",
		"gitVersion" : "fbdef2ccc53e0fcc9afb570063633d992b2aae42"
	},
	"ok" : 1
}

你可能感兴趣的:(MongoDB)