5

I'm attempting to create my own todo list using Javascript, Python and MongoDB. I'm getting stuck on how to handle the task ordering.

My current idea is to have an order field in each task document and when the order changes on the client I would grab the task list from the db and reorder each task individually/sequentially. This seems awkward because large todo lists would mean large amount of queries. Is there a way to update a field in multiple documents sequentially?

I'm also looking for advice as to whether this is the best way to do this. I want to be able to maintain the todo list order but maybe I'm going about it the wrong way.

{
  "_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 Answers 4

2

Mongo is very very fast with queries, you should not be as concerned with performance as if you were using a full featured relational database. If you want to be prudent, just create a todo list of 1k items and try it out, it should be pretty instant.

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

then

db.collection.find( { } ).sort( { order: 1 } )
0

Yes, mongo allows for updating multiple documents. Just use a modifier operation and multi=True. For example, this increments order by one for all documents with order greater than five:

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

As to the best way, usually it's better to use a "natural" ordering (by name, date, priority etc) rather than create a fake field just for that.

3
  • Assuming I grabbed only tasks {"complete": 0} I would have order 1, 4 and 5. Would this then give me order 2, 5 and 6 or would it set the first document's order to 1 and the next would be 2 and the third 3?
    – user830186
    Nov 16, 2012 at 16:58
  • $inc:{x:1} is the same as x += 1 in python (given that x exists).
    – georg
    Nov 16, 2012 at 17:01
  • Ok so it would only increment each tasks order by 1. I want to renumber a tasks order based on what the client did.
    – user830186
    Nov 16, 2012 at 17:01
0

I'm doing something similar. I added a field ind to my list items. Here's how I move a 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

if you're using native promises (es6) in mongoose mongoose.Promise = global.Promise you can do the following to batch:

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)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.