These are chat archives for Automattic/mongoose

3rd
Oct 2017
KARTHIKEYAN A
@karthikeyana__twitter
Oct 03 2017 06:40
db.getCollection('orders').find({"plannedDeliveryDate" : "2017-09-19"})
not working
my date is "plannedDeliveryDate" : "2017-09-19T18:29:00.000Z"
Karthi
@Karthi-SRV
Oct 03 2017 07:55
hi
is there any gitter group for sql?
Vlado Tesanovic
@vladotesanovic
Oct 03 2017 09:16

@karthikeyana__twitter

```db.getCollection('orders').find({"plannedDeliveryDate" : { '$gt': new Date("2017-09-19")}})

```

something like that
@pantchox if you want to have it like ObjectID in database
Arye Shalev
@pantchox
Oct 03 2017 10:50
@vladotesanovic i want to have it working , thats the issue
bumping my issue again please look up ^^^ pasting gist spams the channel :/
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:30
anyone around
?
Nick Rameau
@R4meau
Oct 03 2017 18:31
Just ask what you need.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:31
when using mongoose.save().then((varaible..
does the variable holds the data that was insert into the db?
or I need to request it again using find?
Nick Rameau
@R4meau
Oct 03 2017 18:32
Yes, it's the saved document.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:33
will paste a bit of code gettign a issue
Nick Rameau
@R4meau
Oct 03 2017 18:33
You don't need to.
You already got your answer.
Oh, my bad. It's not working?
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:33
yeah but im doing it and its return an empty variable?
Nick Rameau
@R4meau
Oct 03 2017 18:33
Okay, show me the code.
Normally, you shouldn't be using mongoose.save, but document.save.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:34
in this case model.save
will past code sec
urlShort.findOne({text:original_url}).then((link)=>{
        //url doesnt exist proceed to createOne
        if(!link){
        urlShort.findOne({}, {}, { sort: { 'key' : -1 } }).then((doc) =>{
                return new Promise ((resolve,reject)=>{
                if(doc){
                    resolve(doc.key+1);
                }
                resolve(1);
                });
            }).then((keyUpdate)=>{
                var shortLink = new urlShort({
                    text:original_url,
                    key:keyUpdate
                });
                shortLink.save();
            }).then((docSave)=>{
                response.send(docSave);
            });
        }
        //url exists return the existent
        obj.original_url = original_url;
        obj.short_url = link.key;
        response.send(obj);
    }).catch((err)=>{
        response.send(err);
    });
}).then((docSave)=>{
response.send(docSave);
});
its sending me an empty object
but the data was created with sucess
Nick Rameau
@R4meau
Oct 03 2017 18:35
Because your then is not on the shortLink.save(), it's on the urlShort.findOne.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:36
why and how?sorry got me confusing if you can explain,thanks
Nick Rameau
@R4meau
Oct 03 2017 18:36
I'd recommend you learn some more about promises.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:37
urlShort.findOne({text:original_url}).then((link)=>{
this is the findone promise
if im not wrong?
then*
Nick Rameau
@R4meau
Oct 03 2017 18:38
urlShort.findOne({text:original_url}).then((link)=>{
        //url doesnt exist proceed to createOne
        if(!link){
        urlShort.findOne({}, {}, { sort: { 'key' : -1 } }).then((doc) =>{
                return new Promise ((resolve,reject)=>{
                if(doc){
                    resolve(doc.key+1);
                }
                resolve(1);
                });
            }).then((keyUpdate)=>{
                var shortLink = new urlShort({
                    text:original_url,
                    key:keyUpdate
                });
                shortLink.save().then((docSave)=>{
                response.send(docSave);
            })
            });
        }
        //url exists return the existent
        obj.original_url = original_url;
        obj.short_url = link.key;
        response.send(obj);
    }).catch((err)=>{
        response.send(err);
    });
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:38
need to append it directly that was the problem and you are right thanks mate
Nick Rameau
@R4meau
Oct 03 2017 18:38
You're welcome.
Tiago Correia
@tiagocorreiaalmeida
Oct 03 2017 18:38
have a good one @R4meau
Nick Rameau
@R4meau
Oct 03 2017 18:39
Thanks. You too.
Christoph Diehl
@posidron
Oct 03 2017 18:44
hi, i am quite new to nodejs/mongoose and i do still have some understanding issues. does somebody mind to look over this code and give some advice? it illustrates one schema which has a ref to multiple other documents. which later in another function get populated. may be ignore process.nextTick() (i didn't know how to help myself for now)
exports.add = (req, res, next) => {
  let meta = _.pick(req.body, ['name', 'remote'])
  let repo = new Repository(meta)

  process.nextTick(() => {
    GitManager
      .clone(meta)
      .then(() => {
        GitManager
          .log(meta.name)
          .then((entries) => {
            entries.forEach(function (entry) {
              let commit = new Commit(entry)
              repo.commits.push(commit)
              commit.save()
                .then((object) => { })
                .catch(next)
            })
            repo.status = 'active'
            repo.save()
              .then((object) => { console.log('FINISHED.') })
              .catch(next)
          }).catch(next)
      }).catch(next)
  })

  repo.status = 'pending'
  repo.save()
    .then((entry) => res.json(entry))
    .catch(next)
}
Nick Rameau
@R4meau
Oct 03 2017 19:26
@posidron What exactly is your problem mate?
Christoph Diehl
@posidron
Oct 03 2017 19:31
@R4meau i'm not sure if that's the right way to do it - regarding async. actually since my head is still a bit cranking that i can not launch a new thread to let everything what is inside process.nextTick run in a separate thread/process. (coming from python)
then regarding: "new Commit(entry)" and "repo.commits.push()". is there a way to let that happen in 'batch' kinda way, without that loop?
Christoph Diehl
@posidron
Oct 03 2017 19:37
and regarding process.nextTick, the repository can be quite huge and i want to prevent the main thread from blocking while it gets cloned. is that the right way to do it, or do i really need to setup smth like redis?
it kinda works but it ain't feel right somehow, though i guess i dived with my concerns now more into node/express, rather than mongoose =)
Dennis Belany
@azarus
Oct 03 2017 23:15
you may want to try setImmidiate before process.nextTick
and for the process issue, i'd start a new process for every request like that. And limit the number of processes
if there are too many reuqests just queue them up.
and never run heavy stuff like that in express.
its async and yes its good, but its only async so it can handle many requests without blocking any of them. But that doesn't mean you can put in blocking tasks :)