0

I am running one 'C' application with mongodb. Using mongo shell, I have found a way to retrieve last N records.

But I want to integrate same using C langauge code.

Edit
Using sort() and find() we may get the ascending or descending order data.
One way to limit the output for last N records is to use counter variable with cursor iteration. I am newbie to mongodb and not familiar with BSON objects.

My question is, can I have a similar function for limit() in c language drivers of mongodb (instead of using counter kind of thing)?

Community
  • 1
  • 1
S S
  • 920
  • 1
  • 17
  • 34
  • FYI, your question is likely to be closed or downvoted because it is unclear if you have done much research. You should start with the MongoDB [C driver tutorial](http://api.mongodb.org/c/current/tutorial.html#complex-queries) and the [C driver documentation](http://api.mongodb.org/c/current/index.html).The tutorial includes an example using `find()` and `sort()` which is the same outcome as the other question you linked. You may also be interested in reading up on the [BCON (BSON C Object Notation)](http://api.mongodb.org/c/current/bcon.html) which provides a JSON-like interface for queries. – Stennie Jan 21 '14 at 06:32
  • @Stennie- I wanted to find similar function of `limit()` in c drivers. Please refer edit. In [tutorial](http://api.mongodb.org/c/current/tutorial.html#complex-queries), there is no mention about retrieving last N records. – S S Jan 21 '14 at 09:36
  • What version of the MongoDB C driver are you using? A new version was recently released on Github (Dec, 2013) and there have been some changes between the "legacy" C driver and the new release. FYI, the tutorial and API docs I've linked to are referring to the 0.8.1 "legacy" release which is still the latest stable version. – Stennie Jan 21 '14 at 11:03

1 Answers1

0

Assuming you are using the MongoDB 0.8.1 driver, there is a mongo_find() function to retrieve documents. This is analogous to the mongo shell's find() interface with options for limit and skip:

MONGO_EXPORT mongo_cursor* mongo_find   (mongo * conn,
    const char *    ns,
    const bson *    query,
    const bson *    fields,
    int     limit,
    int     skip,
    int     options 
)

This isn't currently referenced in the C driver tutorial, which uses a lower-level bson_find() interface.

Stennie
  • 63,885
  • 14
  • 149
  • 175
  • Yes, I am using 0.8.1 drivers. [mongo_find()](http://api.mongodb.org/c/current/api/mongo_8h.html#a1889613564f80615e72635307f63592e) helped in understanding it. However, I haven't seen this link anywhere while googling. – S S Jan 23 '14 at 05:02
  • Hrm .. the driver API link (http://api.mongodb.org/c/current/) is referenced in many places including the [MongoDB manual](http://docs.mongodb.org/ecosystem/drivers/c/) and the [mongodb-user group](https://groups.google.com/d/forum/mongodb-user) discussion group. It also seems be the top search result for "mongodb c driver api". Is there a missing location or search term you would suggest? – Stennie Jan 23 '14 at 05:09
  • "MongoDB C Driver Documentation (doxygen generated)" with [this link](http://api.mongodb.org/c/current/api/index.html) on [Tutorial page](http://api.mongodb.org/c/current/index.html) will be helpful and / or provide link to doxygen documentation wherever particular function used in tutorial. The doxygen generated documentation link is only available in some google group posts and some stackoverflow answers. – S S Jan 24 '14 at 08:29