Sign In Register

How can we help you today?

Start a new topic
Answered

Mongodb count with limit

Hi


Can I count with some limit ?

I would like to count new messages after some date. But wanna limit it to 99 to save counting time.

I found https://docs.mongodb.com/v3.2/reference/method/cursor.count/#cursor.count 

and https://docs.mongodb.com/v3.2/reference/method/db.collection.count/

there are some options to do that, but how to do it in cloud code?


ex: Spark.runtimeCollection("doc").count({ts:{$gt:14xxxxxxxxx}}, some options)


Thanks a lot.

Jo


Best Answer

Hi Jo,


In that case you'll probably want to do a count first and then a find. The example below should be close to what you need.

  

//get the count of documents that match the query
var myCount = Spark.runtimeCollection("collectionName").count({ts:{$gt:14xxxxxxxxx}})
    
//if it's <= to 100, limit the results to 100
    if (myCount >= 100){
  var results = Spark.runtimeCollection("collectionName").find({"ts:{$gt:14xxxxxxxxx}}).skip(count).limit(100)
        Spark.setScriptData("results", results)
    }

 

I've included skip here so you can see how to use it.Try that and let us know if you have any further questions. 


Thanks,

Liam


Hi Jo,


Count will return a number so you're better off doing a query on a field and then limiting the results. For example,


var results = Spark.runtimeCollection("customCollection").find({ts:{$gt:14xxxxxxxxx}}).limit(100)

  

It would also be worth indexing the field in the query if you expect this collection to grow over time. You can read about how to index fields here.


Thanks,

Liam

Hi Liam


I just wanna get the number, and if the number greater then 100, just give me 100.

I'm afraid that using find().limit() is heavier than using count(). Am I correct or there's no big diff?


Thanks

Jo

Hi Jo,


If you look at the mongoDB documentation you'll see that using .count() and .find().count() are the same. So here you could use "collection.find(query).limit(limit).count()". If you have any further questions just let us know.


Thanks,

Liam 

Hi  Liam,


That's the point. according to https://docs.mongodb.com/v3.2/reference/method/cursor.count/#cursor.count


"By default, the 
count()method ignores the effects of the cursor.skip() andcursor.limit(). Set applySkipLimit to true to consider the effect of these methods."


but I cannot find a place to set "applySkipLimit" in cloud code api.


Thanks,

Jo

Answer

Hi Jo,


In that case you'll probably want to do a count first and then a find. The example below should be close to what you need.

  

//get the count of documents that match the query
var myCount = Spark.runtimeCollection("collectionName").count({ts:{$gt:14xxxxxxxxx}})
    
//if it's <= to 100, limit the results to 100
    if (myCount >= 100){
  var results = Spark.runtimeCollection("collectionName").find({"ts:{$gt:14xxxxxxxxx}}).skip(count).limit(100)
        Spark.setScriptData("results", results)
    }

 

I've included skip here so you can see how to use it.Try that and let us know if you have any further questions. 


Thanks,

Liam

Login to post a comment