Mayuri
Mayuri

Reputation: 412

Retrieve elements using $slice

My result is following after executing find() query.

{
    "_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"
    }
}

Now i want only any five elements in result(like from 2nd element to 6th element).

How to do this using $slice or is there any other method to retrieve only five elements in result?

Upvotes: 0

Views: 69

Answers (3)

ankit0019
ankit0019

Reputation: 47

If you want to get only 5 record from document you can use limit(), if you want to skip few of the record then use skip().

For Example :

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

This query fetch 5 documents after first 2 documents.

Upvotes: 0

Leo Dagum
Leo Dagum

Reputation: 81

$slice is used to control the number of elements in an array returned by the query, but I don't see an explicit array in your results. Your document should look something like:

{
    "_id" : ObjectId("5384928a03ea2e75268b4567"),
    "fruit" : [
      {
        "name" : "mango",
        "quantity" : "10"
      },
      {
        "name" : "apple",
        "quantity" : "14"
      }
    ]
}

Then you could query it with something like: db.collection.find({},{"fruit": {$slice:[1:6]}})

Upvotes: 0

Neil Lunn
Neil Lunn

Reputation: 151112

The important thing here is, do you actually have an array for $slice to work on? There seems to be a bit of a misconception here common to people working with PHP as the way that language typically represents an array.

The mongo shell ( for example ) will show the clear distinction, where an array actually looks like this in it's JSON representation:

{
    "_id" : ObjectId("5384928a03ea2e75268b4567"),
    "arrayField": [
        {
            "name" : "mango",
            "quantity" : "10"
        },
        {
            "name" : "apple",
            "quantity" : "14"
        },
        {
            "name" : "banana",
            "quantity" : "11"
        },
        {
            "name" : "grapes",
            "quantity" : "19"
        }
    ]
}

That structure can use the $slice operator to return the elements you want. As an example here, 2 documents from index position 1

db.collection.find({},{ "arrayField": { "$slice": [ 1,2] }})

{ 
    "_id" : ObjectId("5384928a03ea2e75268b4567"), 
    "arrayField" : [ 
        { "name" : "apple", "quantity" : "14" }, 
        { "name" : "banana", "quantity" : "11" }
    ]
}

The structure you are showing is just a bunch of key names for sub-documents within the top level document. It is probably a mistake but you just specify those fields in the projection. It's not an array so $slice does not apply:

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

But that probably is not what you want, so it looks like you will need to fix your data so it is an array.

Upvotes: 1

Related Questions