erikolson186 / zangodb

MongoDB-like interface for HTML5 IndexedDB
https://erikolson186.github.io/zangodb/
MIT License
1.07k stars 72 forks source link

Last insert id #19

Open DenQ opened 7 years ago

DenQ commented 7 years ago

Hello. How I can get last added document or last id? I waited similar - https://github.com/dfahlander/Dexie.js/blob/master/samples/react-redux/src/actions/index.js#L27

erikolson186 commented 7 years ago

If you use the findOne method of Collection, without a specified query, the last inserted document should be retrieved.

I see that the MongoDB driver for node provides a way of retrieving this information using the callback of insert: http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insert

I will look into supporting that in the next version of ZangoDB.

Thank you

DenQ commented 7 years ago

Thanks for fast answer. I have tried work with .findOne(), but he give first element from collection. But, I got last element so

collection.insert(docs)
  .then(() => {
    return collection.find().sort({_id: -1}).limit(1).toArray(); //last element wrap array
    // return collection.findOne(); // first element
  })
erikolson186 commented 7 years ago

Good job. Also, I will send a message when the alternative approach is completed.