Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 使用$slice检索元素_Php_Mongodb_Mongodb Query_Nosql - Fatal编程技术网

Php 使用$slice检索元素

Php 使用$slice检索元素,php,mongodb,mongodb-query,nosql,Php,Mongodb,Mongodb Query,Nosql,执行find()查询后,我的结果如下。 { "_id" : ObjectId("5384928a03ea2e75268b4567"), "0" : { "name" : "mango", "quantity" : "10" }, "1" : { "name" : "apple", "quantity" : "14" }, "2" : { "name" : "bana

执行find()查询后,我的结果如下。

{
    "_id" : ObjectId("5384928a03ea2e75268b4567"),
    "0" : {
        "name" : "mango",
        "quantity" : "10"
     },
    "1" : {
        "name" : "apple",
        "quantity" : "14"
     },
    "2" : {
        "name" : "banana",
        "quantity" : "11"
     },
    "3" : {
        "name" : "grapes",
        "quantity" : "19"
     },
    "4" : {
        "name" : "lichi",
        "quantity" : "13"
     },
    "5" : {
        "name" : "orange",
        "quantity" : "10"
     },
    "6" : {
        "name" : "lemon",
        "quantity" : "10"
     },
    "7" : {
        "name" : "pear",
        "quantity" : "10"
     },
    "8" : {
        "name" : "cherry",
        "quantity" : "10"
     },
    "9" : {
        "name" : "kiwi",
        "quantity" : "10"
    }
}
现在我只需要结果中的任意五个元素(如从第二个元素到第六个元素)。

{
    "_id" : ObjectId("5384928a03ea2e75268b4567"),
    "0" : {
        "name" : "mango",
        "quantity" : "10"
     },
    "1" : {
        "name" : "apple",
        "quantity" : "14"
     },
    "2" : {
        "name" : "banana",
        "quantity" : "11"
     },
    "3" : {
        "name" : "grapes",
        "quantity" : "19"
     },
    "4" : {
        "name" : "lichi",
        "quantity" : "13"
     },
    "5" : {
        "name" : "orange",
        "quantity" : "10"
     },
    "6" : {
        "name" : "lemon",
        "quantity" : "10"
     },
    "7" : {
        "name" : "pear",
        "quantity" : "10"
     },
    "8" : {
        "name" : "cherry",
        "quantity" : "10"
     },
    "9" : {
        "name" : "kiwi",
        "quantity" : "10"
    }
}

如何使用$slice执行此操作,或者是否有其他方法仅检索结果中的五个元素?

这里重要的是,您是否确实有一个数组可供处理?对于使用PHP的人来说,这里似乎有一点常见的误解,即语言通常表示数组的方式

mongo shell(例如)将显示出明显的区别,其中数组在其JSON表示中实际上是这样的:

{
“_id”:ObjectId(“5384928a03ea2e75268b4567”),
“arrayField”:[
{
“名称”:“芒果”,
“数量”:“10”
},
{
“名称”:“苹果”,
“数量”:“14”
},
{
“名称”:“香蕉”,
“数量”:“11”
},
{
“名称”:“葡萄”,
“数量”:“19”
}
]
}
该结构可以使用运算符返回所需的元素。这里的一个例子是,索引位置1中有2个文档

db.collection.find({},{“arrayField”:{“$slice”:[1,2]})
{ 
“_id”:ObjectId(“5384928a03ea2e75268b4567”),
“arrayField”:[
{“名称”:“苹果”,“数量”:“14”},
{“名称”:“香蕉”,“数量”:“11”}
]
}
您显示的结构只是顶层文档中子文档的一组键名称。这可能是一个错误,但您只需在投影中指定这些字段。它不是数组,因此不适用:

db.collection.find({},{1:1,2:1,3:1,4:1,5:1,6:1})

但这可能不是您想要的,所以看起来您需要修复数据,使其成为一个数组。

$slice用于控制查询返回的数组中的元素数,但我在结果中没有看到显式数组。您的文档应类似于:

{
    "_id" : ObjectId("5384928a03ea2e75268b4567"),
    "fruit" : [
      {
        "name" : "mango",
        "quantity" : "10"
      },
      {
        "name" : "apple",
        "quantity" : "14"
      }
    ]
}
然后,您可以使用以下内容进行查询:
db.collection.find({},{“fruit”:{$slice:[1:6]})

如果您只想从文档中获取5条记录,可以使用limit(),如果您想跳过几条记录,则使用skip()

例如:

db.collection.find().skip(2)、limit(5)


此查询在前两个文档之后获取5个文档。

您的数据实际上是从mongo shell中获取的,还是以非常PHP的方式抽象出来的。如果这是来自shell,那么数据实际上并不在一个数组中,但这些是以“索引”作为键名的子文档。我通过php将其放入数据库中。为什么这是绝对垃圾?这与这个问题完全无关。哈哈,为了寻找徽章,连载选民。这不是关于
n
记录,而是关于从“单个”文档中提取
n
项的问题。很明显,这本身就有问题。我的个人奖项是本月stackoverflow上最愚蠢的答案。让我们来看看投票结果。请仔细阅读我的问题。