【问题标题】:conversion to PHP, update and insert in subdocument转换为 PHP,在子文档中更新和插入
【发布时间】:2021-05-13 09:40:40
【问题描述】:

我在将语句转换为 php 时遇到问题

这是数据的结构:

db.rooms.find({"_id" : "1620888265"}).pretty();
{
    "_id" : "1620888265",
    "start" : 1620988265,
    "end" : null,
    "users" : [
                {
                "name" : "owner",
                "score" : 0.9,
                "singin" : 1620895469
                },
                {
                "name" : "user",
                "singin" : 1620895769
                }
        ],
    "questions" : [
        {
            "id": 1
            "title" : "X",
            "released" : false
        }
    ]
}

这里是对我有用的命令:

 db.rooms.updateOne({"_id" : "1620970325obn","questions.ident":2},{$set : {"questions.$.released":true,"questions.$.time":'1415'}});

一旦翻译成 PHP(不适合我):

$rooms_db->updateOne(array("_id" => $room,"questions.ident"=>$ident), array ('$set'=>array('questions.$.released' => true,'questions.$.time'=>$time)));

【问题讨论】:

    标签: php mongodb mongodb-query


    【解决方案1】:

    问题是变量“$ident”是一个数字,但由于某种原因,PHP 将其视为字符串。这可以通过var_dump ($ident) 看到。

    "questions.ident"=>(int)$ident),这解决了问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      相关资源
      最近更新 更多