我有一个模型乐队,其中包含旅行列表
Band:
{
name: String,
email: String,
createdAt: String,
tours: Tour[],
...
}
旅游在哪里:
{
name: String,
region: String,
published: Boolean,
...
}
目标只是创建一个接收 Band Name 的端点,Tour Name 会根据此输入删除巡演。
以下作品:
bandService.getBandByName(req.getParam("bandName")).flatMap{ b =>
val tour = b.tours.filter(t => t.name == req.getParam("tourName")).head
mongoDataBaseConnector.bands.findOneAndUpdate(
equal("bandName", req.getParam("bandName")),
pull("tours", tour)
).toFuture().flatMap(u => bandService.getBandByName(req.getParam("bandName")))
但是,这需要我首先通过首先收到的名称解析乐队,过滤,找到游览并将确切的对象传递给拉我试图通过使用pullByFilter来避免这种情况,但似乎无法让它工作. 不幸的是,在 scala 驱动程序中找不到此函数的任何示例。
这就是我正在尝试的:
mongoDataBaseConnector.bands.findOneAndUpdate(
and(
equal("bandName", req.getParam("bandName")),
equal("tours.name", req.getParam("tourName"))),
pullByFilter(and(
equal("tours.$.name", req.getParam("tourName")),
equal("tours.$.region", req.getParam("region"))
))
).toFuture().flatMap(u => bandService.getBandByName(req.getParam("bandName")))
这给出了以下错误:
com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'Cannot apply $pull to a non-array value' on server cluster0-shard-00-01-sqs4t.mongodb.net:27017. The full response is {"operationTime": {"$timestamp": {"t": 1568589476, "i": 8}}, "ok": 0.0, "errmsg": "Cannot apply $pull to a non-array value", "code": 2, "codeName": "BadValue", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1568589476, "i": 8}}, "signature": {"hash": {"$binary": "Qz/DqAdG11H8KRkW8gtvRAAE61Q=", "$type": "00"}, "keyId": {"$numberLong": "6710077417139994625"}}}}
任何想法表示赞赏。这个功能甚至可以实现吗?