Order todo list with MongoDB

I am trying to create my own to-do list using Javascript, Python and MongoDB. I was fixated on how to process a task order.

My current idea is to have an order field in each task document, and when the order changes on the client, I would take a list of tasks from db and reorder each task separately / sequentially. This seems inconvenient because large to-do lists will mean a large number of requests. Is there a way to update a field in multiple documents sequentially?

I am also looking for tips on whether this is the best way to do this. I want to be able to maintain a to-do list, but maybe I will go this route wrong.

{ "_id" : ObjectId("50a658f2cace55034c68ce95"), "order" : 1, "title" : "task1", "complete" : 0 } { "_id" : ObjectId("50a658fecace55034c68ce96"), "order" : 2, "title" : "task2", "complete" : 1 } { "_id" : ObjectId("50a65907cace55034c68ce97"), "order" : 3, "title" : "task3", "complete" : 1 } { "_id" : ObjectId("50a65911cace55034c68ce98"), "order" : 4, "title" : "task4", "complete" : 0 } { "_id" : ObjectId("50a65919cace55034c68ce99"), "order" : 5, "title" : "task5", "complete" : 0 } 
+4
source share
4 answers

Mongo responds very quickly to queries; you should not be as concerned about performance as if you were using a fully functional relational database. If you want to be careful, just create a list of 1k items and try it, it should be pretty fast.

 for (var i = 0; i < orderedListOfIds.length; i++) { db.collection.update({ '_id': orderedListOfIds[i] }, { $set: { order:i } }) } 

then

 db.collection.find( { } ).sort( { order: 1 } ) 
+2
source

Yes, mongo allows you to update multiple documents. Just use the modifier and multi=True . For example, this increases order by one for all documents with order greater than five:

 todos.update({'order':{'$gt':5}}, {'$inc':{'order':1}}, multi=True) 

As for the best way, it is usually best to use a “natural” order (by name, date, priority, etc.) rather than creating a fake field just for that.

0
source

I'm doing something like that. I added an ind field to my list items. This is how I move the list item to a new location:

 moveItem: function (sourceIndex, targetIndex) { var id = Items.findOne({ind:sourceIndex})._id; var movinUp = targetIndex > sourceIndex; shift = movinUp ? -1 : 1; lowerIndex = Math.min(sourceIndex, targetIndex); lowerIndex += movinUp ? 1 : 0; upperIndex = Math.max(sourceIndex, targetIndex); upperIndex -= movinUp ? 0 : 1; console.log("Shifting items from "+lowerIndex+" to "+upperIndex+" by "+shift+"."); Items.update({ind: {$gte: lowerIndex,$lte: upperIndex}}, {$inc: {ind:shift}},{multi:true}); Items.update(id, {$set: {ind:targetIndex}}); } 
0
source

if you use the built-in promises (es6) in mongoose mongoose.Promise = global.Promise you can do the following:

 function batchUpdate(res, req, next){ let ids = req.body.ids let items = [] for(let i = 0; i < ids.length; i++) items.push(db.collection.findOneAndUpdate({ _id:ids[i] }, { $set: { order:i } })) Promise.all(items) .then(() => res.status(200).send()) .catch(next) } 
0
source

Source: https://habr.com/ru/post/1446494/


All Articles