These are chat archives for Automattic/mongoose

28th
Oct 2015
Chetan Dhembre
@chetandhembre
Oct 28 2015 06:36
hi what is difference between mongoose version 3.8.36 and 4.x.x. ?
Can any one tell me
James Lee
@uptownhr
Oct 28 2015 15:35
@chetandhembre don't know off the top of my head, but checkout https://github.com/Automattic/mongoose/wiki/4.0-Release-Notes
Christo Mitov
@christomitov
Oct 28 2015 19:03
hello, im trying to do a sub document query with mongoose.. in mongo terminal: db.queueitems.find({'user._id': ObjectId('56311a292625f11f80e6cf4b'), 'toWatch': true}) , this gets me what i want
but when i do it with mongoose it returns nothing
my query is like var query = { 'user._id': new ObjectId(req.params.userId), 'toWatch': true};
Edward Hotchkiss
@edwardhotchkiss
Oct 28 2015 19:22
Hey all, any idea why why MySchema.pre('update', function(next) { this.some_field = 'whatever'; }); logs this correctly yet fails to save the logged update? mongoose 4.1.
Christo Mitov
@christomitov
Oct 28 2015 19:25
@edwardhotchkiss have you done a nested document query before by id?
James Lee
@uptownhr
Oct 28 2015 19:27
@christomitov in mongoose, not sure if this will help, mongoose will translate the id string to OjbectId
{'user._id': req.params.userId} should work
@edwardhotchkiss any chance some_field is a sub doc?
Christo Mitov
@christomitov
Oct 28 2015 19:29
@uptownhr i tried that, heres what the data looks like, sec: http://pastebin.com/00wPf2CV
James Lee
@uptownhr
Oct 28 2015 19:30
does it work when you query against user.provider?
Christo Mitov
@christomitov
Oct 28 2015 19:30
ive tried without ObjectId casting as well
um sec
James Lee
@uptownhr
Oct 28 2015 19:32
is, console.log returning null?
Christo Mitov
@christomitov
Oct 28 2015 19:32
@uptownhr i can't query provider either
looks like i can't query the subdocument, period.
@uptownhr no, i get an empty array
do you want to see the schema as well or?
James Lee
@uptownhr
Oct 28 2015 19:33
yes please
Christo Mitov
@christomitov
Oct 28 2015 19:33
maybe i did that wrong
James Lee
@uptownhr
Oct 28 2015 19:35
can i see the user schema as well?
Christo Mitov
@christomitov
Oct 28 2015 19:36
James Lee
@uptownhr
Oct 28 2015 19:38
looks ok.. can you show the console.log of req.params
Christo Mitov
@christomitov
Oct 28 2015 19:39
REQUEST PARAMETERS: { userId: '56312289968453758233f92f',
added: '2015-10-28T19:32:23.864Z' }
James Lee
@uptownhr
Oct 28 2015 19:40
does this exist?
Christo Mitov
@christomitov
Oct 28 2015 19:40
@uptownhr db.queueitems.find({'user._id': ObjectId('56312289968453758233f92f'), 'toWatch': true})
that returns stuff
even without the toWatch part actually.
thats in mongo console though.
James Lee
@uptownhr
Oct 28 2015 19:41
should be working
Christo Mitov
@christomitov
Oct 28 2015 19:42
@uptownhr trying again without the explicit ObjectId cast
ya no.. empty array
and mongo console query works.
the user id changes everytime because when i save express restarts itself , wipes the db and put ssome seed data there
James Lee
@uptownhr
Oct 28 2015 19:44
ic
Christo Mitov
@christomitov
Oct 28 2015 19:44
in case you're notingi a descrepancy in userid
pretty much established mongoose is the culprit here though.. as the vanilla mongo query works fine
James Lee
@uptownhr
Oct 28 2015 19:45
yea very strange
Christo Mitov
@christomitov
Oct 28 2015 19:45
what do you recommend
James Lee
@uptownhr
Oct 28 2015 19:46
@vkarpov15 :) can you help
sorry, looks to be checking out for me
Christo Mitov
@christomitov
Oct 28 2015 19:46
is there any subtle crap i should be aware of
i really thought the objectId was the issue
James Lee
@uptownhr
Oct 28 2015 19:47
so when you queried against another field does it work?
like provider: 'local'?
Christo Mitov
@christomitov
Oct 28 2015 19:49
@uptownhr no.. thats why i stopped worrying about ObjectId
it doesnt treat user as a subdocument
or im unable to query it properly
James Lee
@uptownhr
Oct 28 2015 19:50
let me think about this
Christo Mitov
@christomitov
Oct 28 2015 19:50
ok
its like, you get in your car and drive off
:(
Christo Mitov
@christomitov
Oct 28 2015 19:56
@uptownhr ya im running like var query = {
'user.provider': 'local'
};
and i know its local in the db, im looking at the 2 documents in MongoHub
nothing coming up.
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:16
I set Mongoose to use bluebird promises, then I tried to create a function for login:
function login (email,password) {

    return Account.findOne({email: email}, function (err, user) {
        console.log("hola");
        if (err) {
            console.log("Find err " + err);
            throw err;
        }

        if (!user)
            return {notFound: true}

        return user.validPassword(password, function (err, match) {

            console.log("happens");

            if (err) {
                console.log("password err " + err);
                throw err;
            }

            user.matches = true;
        });
    });
}
James Lee
@uptownhr
Oct 28 2015 20:17
@christomitov sorry chris
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:18
so the problem I'm having is that I can't return the promise with matches property set in user object
even I'm never seeing "happens" logged to console
James Lee
@uptownhr
Oct 28 2015 20:19
@diegoaguilar i don't believe returning inside the callback propogates
Christo Mitov
@christomitov
Oct 28 2015 20:19
@uptownhr what does that mean :(
James Lee
@uptownhr
Oct 28 2015 20:20
@christomitov will you try without a sub document?
just a schema type with Mixed
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:20
I just need to find a way to make my login method return a promise which has evaluated a match first
any idea?
James Lee
@uptownhr
Oct 28 2015 20:20
@diegoaguilar the first return
doesn't return the promoise of user.validPassword
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:21
.. you're right
James Lee
@uptownhr
Oct 28 2015 20:22
because the callback is not returned as a promise
you'll need to create your own promise and return that
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:23
could u guide me a bit, I way too consufed up to this poiint
James Lee
@uptownhr
Oct 28 2015 20:25
user.validPassword returns what exactly?
and is the callback required for that method?
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:26
    accountSchema.methods.validPassword = function(password) {
        return bcrypt.compareSync(password, this.password);
    }
James Lee
@uptownhr
Oct 28 2015 20:26
validPassword doesn't do anything asynchronous right?
ok
so for one, there is no callback
that is why 'happens' never happens :)
just add the console.log inside validPasswords method
Christo Mitov
@christomitov
Oct 28 2015 20:31
@uptownhr something else is screwed up... i just tried adding a user_id field . of type string
and just saving the string of the id there
i cant find it by that either..
James Lee
@uptownhr
Oct 28 2015 20:33
to the sub doc or main doc?
is any query working?
actually, what does QueuItem = too?
Christo Mitov
@christomitov
Oct 28 2015 20:36
let me console log it
James Lee
@uptownhr
Oct 28 2015 20:37
so for before
in regards to the schema
i did find an error
you are currently getting the usermodel
User Model
Christo Mitov
@christomitov
Oct 28 2015 20:38
@uptownhr ok..
what do i do
James Lee
@uptownhr
Oct 28 2015 20:38
but you need the schema instead
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:38
@uptownhr I did simplify it to this, as validPassword is sync
    return Account.findOne({email: email}).then(function (user) {
        user.match = user.validPassword(password);
        return user;
    });
still , user has NOT match property
James Lee
@uptownhr
Oct 28 2015 20:40
do you get a user object?
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:41
yep
James Lee
@uptownhr
Oct 28 2015 20:41
can you show me that code
Christo Mitov
@christomitov
Oct 28 2015 20:42
@uptownhr im getting the user model tho
var UserSchema = require('../user/user.model');
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 20:42
@uptownhr what do you want to see
James Lee
@uptownhr
Oct 28 2015 20:43
@christomitov but you should be getting the schema
actually, i'm not 100% about this
Christo Mitov
@christomitov
Oct 28 2015 20:43
im using yeoman generator..
that should be fine.
James Lee
@uptownhr
Oct 28 2015 20:43
hmm ok
Christo Mitov
@christomitov
Oct 28 2015 20:43
and its saving appropriately, its something else
James Lee
@uptownhr
Oct 28 2015 20:44
so are you able to run any queries?
Christo Mitov
@christomitov
Oct 28 2015 20:45
im just gonna move on for now
shits due friday and i need other things to work
James Lee
@uptownhr
Oct 28 2015 20:45
sorry man
very strange
Christo Mitov
@christomitov
Oct 28 2015 20:45
its all good , thanks for trying anyway
James Lee
@uptownhr
Oct 28 2015 20:45
looks like you are doing everything fine
but try var q = { toWatch: true }
see if that returns anything
i'm starting to think it's the added query that is messing it up
i should have asked to see your query param
can you disable, query.added = ...
Edward Hotchkiss
@edwardhotchkiss
Oct 28 2015 21:28
@uptownhr nah its not a sub doc. apparently mongoose 4 changed a bit of api :/
Cameron Roe
@cameronroe
Oct 28 2015 22:05
Hi all, had a quick question about testin models with mongoose
For some reason, I’m following this gist https://gist.github.com/nkzawa/4971592 and I’m not able to get mongoose to connect.
Getting Error: Could not connect to MongoDB. Did you forget to runmongod?
Clearly have it running.
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 22:16
Inside the callback body of a find or findOne I'm trying to do a delete to a property on the found record
Luke A. Greenleaf
@gishmel
Oct 28 2015 22:17
why not just do a projection?
Diego Aguilar Aguilar
@diegoaguilar
Oct 28 2015 22:17
I dont understand @gishmel
Luke A. Greenleaf
@gishmel
Oct 28 2015 22:19
you can project what the find command should produce for you
so I believe you can just pass a second object and any field you don't want to show up give it as the name of the field: 0
so its like example.find({id: n}, {fieldToBeOmitted: 0})
or do you actually want to delete the property?
cause you can't find and delete a single property I mean you can find a document and then resave it after modifying it such as deleting the one property but if you are just trying to have the client not see some part of the database I believe the solution is projection as stated above
Luke A. Greenleaf
@gishmel
Oct 28 2015 22:24
does that make sense?
James Lee
@uptownhr
Oct 28 2015 22:31
@cameronjroe probably due to localhost not being where your mongodb is at