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
C
Customer Support
said
about 6 years ago
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.
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
J
Jo Huang
said
about 6 years ago
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
Customer Support
said
about 6 years ago
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.
"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
Customer Support
said
about 6 years ago
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.
Jo Huang
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
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.
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
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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,
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
Jo Huang
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
Customer Support
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
Jo Huang
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
Customer Support
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.
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
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics