我想通过responsaveis.$
([email protected])获取数组的特定元素,但是没有结果,我的语法有问题吗?
{
"_id" : ObjectId("54fa059ce4b01b3e086c83e9"),
"agencia" : "Abc",
"instancia" : "dentsuaegis",
"cliente" : "Samsung",
"nomeCampanha" : "Serie A",
"ativa" : true,
"responsaveis" : [
"[email protected]",
"[email protected]"
],
"email" : "[email protected]"
}
语法1
mongoCollection.findAndModify("{'responsaveis.$' : #}", oldUser.get("email"))
.with("{$set : {'responsaveis.$' : # }}", newUser.get("email"))
.returnNew().as(BasicDBObject.class);
语法2
db.getCollection('validatag_campanhas').find({"responsaveis.$" : "[email protected]"})
结果
Fetched 0 record(s) in 1ms
最佳答案
$
位置运算符仅在update(...)或项目调用中使用,您不能使用它返回数组中的位置。
正确的语法是:-
语法1
mongoCollection.findAndModify("{'responsaveis' : #}", oldUser.get("email"))
.with("{$set : {'responsaveis.$' : # }}", newUser.get("email"))
.returnNew().as(BasicDBObject.class);
语法2
db.getCollection('validatag_campanhas').find({"responsaveis" : "[email protected]"})
如果只想投影特定元素,则可以在投影中使用位置运算符
$
作为{“ responsaveis。$”:1}
db.getCollection('validatag_campanhas').find({"responsaveis" : "[email protected]"},{"responsaveis.$":1})