如何用MID查询mongodb
我有查询的东西像SQL的查询:
select * from users where MID(nama,1,2)='A'
如何在mongodb中做同样的事情? 我无法在文档中找到运营商。
尝试以下聚合查询:
db.users.aggregate([{$project: { _id: 0, name: { $substr: [ "$name", 0, 1 ]}}}])
这使用预测和子字符串:https://docs.mongodb.com/v3.2/reference/operator/aggregation/project/ https://docs.mongodb.com/v3.2/reference/operator/aggregation/substr /
例如:
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db.users.save({"name": "Kevin"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Matt"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Sakis"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Lee"})
WriteResult({ "nInserted" : 1 })
>
> db.users.aggregate([{$project: { _id: 0, name: { $substr: [ "$name", 0, 1 ]}}}])
{ "name" : "K" }
{ "name" : "M" }
{ "name" : "S" }
{ "name" : "L" }
链接地址: http://www.djcxy.com/p/86407.html