【问题标题】:MongoDb Laravel where clauseMongoDb Laravel where 子句
【发布时间】:2017-11-30 02:24:52
【问题描述】:

在 MongoDb 中,我有一个名为“common_id”的字段的文档。我只想获取特定 common_id 的所有文档。

这就是 common_id 字段的样子。

"common_id" : ObjectId("5911af8209ed4456d069b1d1"),

我试过了,

$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', '=', 'true')
->get();

这个,

$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', '=', 'ObjectId("5911af8209ed4456d069b1d1")')
->get();

还有这个,

MasterAffiliateProductMappingMongo::where('common_id', '=', ObjectId("5911af8209ed4456d069b1d1"))
    ->get();

也试过这个,

$bestAmongAffiliates = MasterAffiliateProductMappingMongo::where('common_id', new MongoDB\BSON\ObjectID('5911af8209ed4456d069b1d1'));

我认为问题在于 - ObjectId("5911af8209ed4456d069b1d1") 不是字符串,这就是上述代码不起作用的原因。反正我不确定。 但没有任何效果。我该怎么做。

【问题讨论】:

  • MasterAffiliateProductMappingMongo::where('common_id', ObjectId("5911af8209ed4456d069b1d1")) 当然。而且我认为您实际上应该从MongoDB\BSON\ObjectId 拉入ObjectId
  • 老兄,这正是我给你 BSON 库函数链接的原因,你需要要导入。
  • @NeilLunn 哦....酷。 :) 谢谢

标签: mongodb laravel-5 eloquent where database


【解决方案1】:
::where('common_id', '5911af8209ed4456d069b1d1')->get()

如果您使用的是 Jenssegers Laravel MongoDB,应该可以使用。

检查它是否 string 和字符串是 ctype_xdigit 然后在查询生成器中转换为对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 2017-08-05
    • 2020-11-28
    • 1970-01-01
    • 2017-02-10
    • 2020-09-28
    • 2013-06-30
    • 2018-09-05
    相关资源
    最近更新 更多