You are not logged in.
Hi!
I'm using mOROot for my private project.
And i want to know how to fetch the latest document from mongoDB server with Direct Access synMongoDB
In googling, i can find some command for mongoDB
db.foo.find().sort({$natural:1}).limit(1);
How to implement above command to FindDoc or FindJSON like below:
Client := TMongoClient.Create('host');
DB := Client.Database['dbname'];
Coll := DB.CollectionOrCreate['collect'];
Coll.FindDoc('{ $sort: {_id : -1 } } ', []);
help me please.
Offline
You should use TMongoCollection.AggregateDoc() (or AggregateJSON).
/// calculate aggregate values using the MongoDB aggregation framework
// and return the result as a TDocVariant instance
// - the Aggregation Framework was designed to be more efficient than the
// alternative map-reduce pattern, and is available since MongoDB 2.2 -
// see http://docs.mongodb.org/manual/referenc … /aggregate
// - you should specify the aggregation pipeline as a list of JSON object
// operators (without the [..]) - for reference of all available phases,
// see http://docs.mongodb.org/manual/core/agg … n-pipeline
// - if the server sent back no {result:...} member, will return null
// - if the server sent back one item as {result:[{..}]}, will return
// this single item as a TDocVariant
// - if the server sent back several items as {result:[{..},{..}]}, will
// return a dvArray kind of TDocVariant
// - for instance, the following will return the maximum _id value of
// the collection:
// ! AggregateDoc('{$group:{_id:null,max:{$max:"$_id"}}}',[]).max
function AggregateDoc(Operators: PUTF8Char; const Params: array of const): variant;
Then you specify the $sort argument, possibly:
AggregateDoc('{ $sort : { $natural : 1 } }, { $limit : 1 }',[])
Offline
Thanks for your advice.
But when i execute as below:
Doc := Coll.AggregateDoc('{ $sort : { $natural : 1 } }, { $limit : 1 }',[]);
Null is returned to Doc variable.
Of course, Doc := Coll.FindDoc(); is OK!
What is the problem for getting the document?
Give me more advice, please.
Thanks in advance.
Offline
I have solved above problem as:
Doc := Coll.AggregateDoc('{ $sort : { _id: 1 } }, { $limit : 1 }',[]);
not $natural but _id.
Thanks.
Offline
Weird.
http://docs.mongodb.org/manual/referenc … ta/natural states that it should work.
But, by design, _id are generated in increasing order...
As a consequence, your query also does the trick.
BTW why are you searching for the latest document ID?
Usually, you have it before adding the document, on client side, since the _id is generated before sending the document to the server.
Either you explicitly set the _id field before calling Insert or Save, or you have the CreatedObjectID: PBSONObjectID optional parameter in our TMongoCollection.Insert() and TMongoCollection.Save() methods to get the generated ID.
Offline
I do make some device's monitoring program.
The device status data is saved using TMongoCollection.Insert method by client #1 program to MongoDB in every second.
And the client #2 query the saved data which last inserted document and display them to monitor.
I'm not just want a document ID but a last inserted data.
Offline