Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用引用其他文档的Doctrine2查询MongoDB嵌入文档_Mongodb_Doctrine Orm_Doctrine Odm - Fatal编程技术网

无法使用引用其他文档的Doctrine2查询MongoDB嵌入文档

无法使用引用其他文档的Doctrine2查询MongoDB嵌入文档,mongodb,doctrine-orm,doctrine-odm,Mongodb,Doctrine Orm,Doctrine Odm,基本上,我正在尝试运行以下查询,因为它在命令行上运行时返回正确的结果 db.order_in_progress.find({ "orderProducts.carrierAccount.$id" : "50db39141311fa9421000000" }); 但是,我无法使用doctine2创建正确的查询。我目前的查询如下: $qb = $this->createQueryBuilder(); $qb->find($this->getClassName()); $qb-&g

基本上,我正在尝试运行以下查询,因为它在命令行上运行时返回正确的结果

db.order_in_progress.find({ "orderProducts.carrierAccount.$id" : "50db39141311fa9421000000" });
但是,我无法使用doctine2创建正确的查询。我目前的查询如下:

$qb = $this->createQueryBuilder();
$qb->find($this->getClassName());
$qb->field('orderProducts.carrierAccount.$id')->equals(new \MongoId($carrierAccount->getId()));
return $qb->getQuery()->execute();
这将导致运行以下查询

db.order_in_progress.find({ "orderProducts.carrierAccount": ObjectId("50db39141311fa9421000000") })
如果我删除了\MongoID内容,则会得到以下查询。这更接近,但它正在截断字段名中的“$id”部分

db.order_in_progress.find({ "orderProducts.carrierAccount": "50db39141311fa9421000000" })
相关的父文档(我正在查询的文档)如下所示。(我已经删除了不相关的字段)


救命啊

嗯。首先,你确定你使用的是嵌入文档而不是引用多个carrierAccount文档吗?看起来是这样的,因为嵌入文档的目的是为了不存储任何引用以便更快地阅读。不太清楚为什么它不允许您在querybuilder中使用$id。但是,如果您正在使用引用,并且想要快速修复和一些优化,您可以简单地使orderProducts引用carrierAccounts。通过使用@ODM\ReferenceMany(targetDocument=“CarrierAccount”,simple=true),我在订单文档中嵌入orderproduct对象1次以上。orderproduct可以有一个对运营商帐户的引用。
{
  "_id" : 148,  
  "orderProducts" : [{      
      "carrierAccount" : {
        "$ref" : "carrier_account",
        "$id" : ObjectId("50db39141311fa9421000000"),
        "$db" : "dev"
      },      
    }, {
      "carrierAccount" : {
        "$ref" : "carrier_account",
        "$id" : ObjectId("50db39141311fa9421000000"),
        "$db" : "cerberus_dev"
      }],
}