Reputation: 23548
I'm going over a project I've taken over which is crashing in this doctrine/mongoDB query:
/**
* Deactivate all Device documents via set 'activate = false' and unsetting
* passenger field for all devices that has passenger = $oPassenger
* and Device._id != oDevice
* @param Document\Device $oDevice
* @param Document\Passenger $oPassenger
*/
public function deactivateDuplicateDevices(Document\Device $oDevice,
Document\Passenger $oPassenger)
{
$passengerId = new \MongoId($oPassenger->getId());
$deviceId = new \MongoId($oDevice->getId());
return $this->createQueryBuilder('Device')
->update()
->multiple(true)
->field('activated')->set(false)
->field('passenger')->unsetField()->equals($passengerId)
->field('_id')->notEqual($deviceId)
->getQuery()
->execute();
}
it seems to me that the author is running multiple queries on the same document ie find all documents where
passenger == passengerId AND
device != deviceId
I'm trying to recreate this query piece by piece (in JSON and running it on the mongo console) to see where the problem is. this is the final input I got (to the mongo initiated, this may seem absurd.. but please bear with me):
db.Device.update(
{
passenger:{
$ne: "538c6eac3c0ab224080041aa"
}
},
{
_id:{
$ne: "538eb8d205dafff40a0041ad"
}
}
{
$unset:{
passenger:""
}
},
{
$set:
{
activated:false
}
}
)
I wanted to test the first part, namely the multiple queries (obviously searching for passenger alone and _id alone work.. but when i combine them):
db.Device.find({
_id:{
$ne:ObjectId("538eb8d205dafff40a0041ad")
}
},
{
passenger:{
$ne:"538c6eac3c0ab224080041aa"
}
}
)
I get this error:
error: {
"$err" : "Can't canonicalize query: BadValue Unsupported projection option: passenger: { $ne: \"538c6eac3c0ab224080041aa\" }",
"code" : 17287
}
any idea what i'm doing wrong here?
Upvotes: 2
Views: 2376
Reputation: 547
You can simply do
db.Device.find({
_id: {$ne: ObjectId("538eb8d205dafff40a0041ad")},
passenger: {$ne: "538c6eac3c0ab224080041aa"}
})
or
db.Device.update({
_id: {$ne: ObjectId("538eb8d205dafff40a0041ad")},
passenger: {$ne: "538c6eac3c0ab224080041aa"}
},
{
$unset: {passenger: 1},
$set: {activated: false}
})
Note that there is only one query object argument and one update object argument. There are other optional arguments you can check out in the docs.
Upvotes: 2
Reputation: 15119
You should use $and
(http://docs.mongodb.org/manual/reference/operator/query/and/)
db.Device.find({ $and: [ {
_id:{
$ne:ObjectId("538eb8d205dafff40a0041ad")
}
},
{
passenger:{
$ne:"538c6eac3c0ab224080041aa"
}
} ] }
)
Upvotes: 2