These are chat archives for Automattic/mongoose

30th
Nov 2017
Mariano Zunino
@forbi
Nov 30 2017 01:36
Hi ppl! I need some help here. Im writing an api backend wich implementes mongooose :clap:
Right now im facing this problem:
There's a request which does a bunch of work with mongo
     //Creates a pretty big array of Ops
     let bulkUpdateOps = articles.map(function(article) {
      //First i need some stuff from A
       return A.findOneAndUpdate(
            query
        )
        .then(function() {
        //Thne some from B
          return B.findOneAndUpdate(
            query
          );
        })
        .then(function(result) {
         //now with result I can build the op and return it
          return {
            updateOne: {
              filter: {
               query
              },
              update: {
                data
              },
              upsert: true
            }
          };
        })
        .catch(function(err) {
          console.log(err);
          return res(Boom.badImplementation("Error: ", err));
        });
    });
    //Now that i have the arra of Ops, let fucking execute it
    Promise.all(bulkUpdateOps)
      .then(function(ops) {
        return C.bulkWrite(ops, {
          ordered: true,
          w: 1
        });
      })
      .then(_ => {
        return res("Imported");
      })
      .catch(function(err) {
        console.log(err);
        return res(Boom.badImplementation("Error: ", err));
      });
While this function is runnig, is like mongo is locked or somthing. I can't make another request to the api until this function is completed.
How I make the connection:
var db = "mongodb://" + dbURL + ":" + dbPort + "/" + dbName;
Mongoose.connect(db, {
    useMongoClient: true
}).then...
shooterbret
@shooterbret
Nov 30 2017 03:30
mongo queues queries executing 1 at a time im pretty sure unless you are using multiple DB's
Mariano Zunino
@forbi
Nov 30 2017 11:31
@shooterbret I kind of solve it using indexes. One of the query, was searching to many fields at the same time and text based. I made some indexes an now the response time improved like 800% (no kidding)
Juha Lindstedt
@pakastin
Nov 30 2017 17:47
Try add .lean() after query 😉
Mariano Zunino
@forbi
Nov 30 2017 17:48
@pakastin Also implemented .lean() :D
Juha Lindstedt
@pakastin
Nov 30 2017 17:49
👍