skip to Main Content

I have a mongodb that is accessed through an API via the "requests" package. Within the database there are numerous schema we’ve set up; as an example, one of these is a "surveys" collection in which documents each listing different survey parameters are stored.

I have been using requests.get() in what I believe to be a standard way, for instance to access a survey with document ID "blah":

requests.get("http://ipaddress:port/surveys/blah")

If I instead wished to only query the most recently submitted document to the survey collection, how should I alter my "requests" command?

PS Apologies if I have misrepresented the code set-up with misused terms, I’m a scientist who does a bit of code and not the other way around.

I think it might work to request the whole collection, then sort by entry date, but this seems like a waste of transfer bandwidth (the whole database is quite large). I am hoping to find a more direct solution that only returns one document rather than all.

I have found solutions on stackoverflow using Meteor or directly using the MongoDB communications interface but prefer requests to keep my code usage self-consistent.

2

Answers


  1. Let me restate your question to make sure I’m understanding it right:

    You have a web API that accesses MongoDB (or whatever database, it doesn’t really matter, you can do similar things with any of them). You would like to know the best way to modify the API so it can return the latest version of the "Surveys" collection similar to how you query the document "blah"? It doesn’t seem like you’re querying Mongo directly through the requests library, since I don’t think Mongo supports requesting the data that way.

    There are multiple approaches you could take structuring the API. First, you could have a document that has the ID "latest" and access it like this: /surveys/latest. Another, probably better approach, would be to query the Survey’s collection, sort by update timestamp (assuming that’s how you want the data) and limit the results to 1 document: ie, /surveys/?sort=updateTs&limit=1. You can do this in the backend similarly: running the find_one() command on the collection and giving the sort option with the field of the timestamp and the direction "descending". If you have an index on the timestamp field, this query will be super efficient.

    If I’ve misunderstood your question and you would like to receive only a part of a single document in a MongoDB query, you can use the projection parameter to limit the part of the document that is returned by the API. The URL might look like this /surveys/blah?fields=surveyVersions.0,surveyQuestions or just /surveys/blah?version=latest depending on how you want it structured. On the backend you would use a command like find_one("blah", projection={"documents": {"slice":-1}}) to return the last item in the documents array of the "blah" document.

    I’m using pymongo in these examples, since you said you were using python.

    Login or Signup to reply.
  2. MongoDB offers a pseudo-index named "$natural". This "index" considers documents in the order they were inserted into the database, without regard to their content or any subsequent updates.

    If you specify a sort of {"$natural": -1} documents will be returned in the reverse order they were inserted, so a query like find().sort({"$natrual": -1}).limit(1) should very quickly return the most recently inserted document.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search