Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Php Mongod DB使用条件更新数组元素顺序_Php_Arrays_Json_Mongodb_Mongodb Query - Fatal编程技术网

Php Mongod DB使用条件更新数组元素顺序

Php Mongod DB使用条件更新数组元素顺序,php,arrays,json,mongodb,mongodb-query,Php,Arrays,Json,Mongodb,Mongodb Query,我想用条件对Mongodb数组元素进行排序 假设我的数组是 { "id":1, "array":[ { "id" : "150", "place" : "US" }, { "id" : "1250", "place" : "UK" }, { "id" : "1350",

我想用条件对Mongodb数组元素进行排序

假设我的数组是

{
   "id":1,
   "array":[
        {
           "id" : "150",
           "place" : "US"  
        },
        {
           "id" : "1250",
           "place" : "UK"  
        },
        {
           "id" : "1350",
           "place" : "AUS"  
        }
   ]    
}
然后我想对“数组”进行排序和更新 我想按12501350150的顺序排列数组元素值“id”

我的更新文档看起来像

{
   "id":1,
   "array":[
       {
           "id" : "1250",
           "place" : "UK"  
        },
        {
           "id" : "1350",
           "place" : "AUS"  
        },
        {
           "id" : "150",
           "place" : "US"  
        },
   ]    
}
将运算符与和修改器一起使用

db.collection.update({"id":1},
                     {$push:{"array":{$each:[],$sort:{"id":1}}}})
数组传递给
$each
修饰符,可确保不添加其他元素,并且仅根据
id
字段按升序词汇顺序对现有元素进行排序(因为
id
是字符串类型)

要使用
$sort
修饰符,它必须与
$each
修饰符一起出现。你 可以将空的
数组[]
传递给
$each
修饰符,以便只有
$sort
修饰符有效果

如果您希望根据id字段上的某个优先级对其进行排序,则可以在应用程序代码中进行排序,如下所示:

// define an array of elements with decreasing priority
var sortOrder = ["1250","1350","150"];

// find and update all the documents
db.collection.find().forEach(function(doc){
    var arr = doc.array.sort(function(a,b){
        return (sortOrder.indexOf(a.id) - sortOrder.indexOf(b.id));
    })
    db.collection.update({"_id":doc._id},{$set:{"array":arr}});
})
但我建议最好的方法是在插入文档或更新
数组
字段时保持顺序

id
字段创建映射,以确定元素在数组字段中的排序顺序

var sortOrder = ["1350","1250","150"];
每当您想要插入/更新时,请使用操作符执行操作,该操作符将新元素插入/更新数组字段,并基于排序器将新元素置于相应位置

var elementToInsert = {"id":"1350","place":"UK"};
db.collection.update({"id":1},
            {$push:{"array":{$each:
                        [elementToInsert],
                      $position:sortOrder.indexOf(elementToInsert.id)}}},
            {"upsert":true})

是否要将数组的第一个元素推到最后一个右侧?按1旋转?不是旋转,我想按照idI的给定值顺序排列,但我想按照给定的id顺序排列,就像我想让id值为1250的元素保持在第一个位置,id值为1350的元素保持在第二个位置,150保持在第三个位置,那么我应该怎么做呢?如果在上面的解决方案中,您不能使用
$sort
参数指定所需的顺序,那么您需要检索文档,修改数组客户端以使其处于适当的顺序,然后使用新的、重新引用的数组字段更新数据库中的文档。
var elementToInsert = {"id":"1350","place":"UK"};
db.collection.update({"id":1},
            {$push:{"array":{$each:
                        [elementToInsert],
                      $position:sortOrder.indexOf(elementToInsert.id)}}},
            {"upsert":true})