These are chat archives for Automattic/mongoose

17th
Jun 2015
Varmint
@bonbonio
Jun 17 2015 00:45
hi all
just a quick question if anyone is around?
ArticleSchema.index({ club_id: 1, url: -1 }); i want two fields to combine to be a composite index as two clubs could share the same url.
a) what does the 1 and -1 refer to, and b) it doesnt work, both fields in the schema are not defined as indexes when creating the schema, this bit is set at the end
club_id: {
lowercase: true,
required: true,
type: String,
trim: true
}, and the same is set for url
the problem is, the index isnt enforced, save allows duplicate entries with same club_id and url.
Varmint
@bonbonio
Jun 17 2015 01:00
nevermind.
a silly error.
still unsure what 1 and -1 refer to but ill find out
Mark Herhold
@MarkHerhold
Jun 17 2015 01:01
@varmint09 +/-1 refers to the order the docs are indexed in... ascending vs descending order.
Varmint
@bonbonio
Jun 17 2015 01:06
yes just managed to find this on mongo
i should check more often on mongodb rather than only google mongoose
thank you
ill set both to 1 as they are strings
many thanks all.
Mark Herhold
@MarkHerhold
Jun 17 2015 01:09
np, glad I could help! If you are searching on the club_id field in any order, { club_id: 1, url: 1 } is a good index. If you are searching on both fields in ascending order, that index is again a good pick. If you ever need to search on club_id in ascending order AND url in descending order, that index is NOT a good pick.
Stephan Kristyn
@nottinhill
Jun 17 2015 07:34
@vkarpov15 thank you for the Testing hints!
Varmint
@bonbonio
Jun 17 2015 13:07
Hello again
because people were so helpful this (early) am, I am going to pose another issue ive been having where a solution is unclear
its about an objectid failing when i do a findOneAndReplace upsert
i can post a gist if anyone is alive to look? :)
Varmint
@bonbonio
Jun 17 2015 13:19
ill be on for hours so can wait.
Erinç Fırtına
@EricMcRay
Jun 17 2015 13:25
post it. I am sure someone check that
Varmint
@bonbonio
Jun 17 2015 13:35
ok
Erinç Fırtına
@EricMcRay
Jun 17 2015 13:41
I havent use Q for promises I use Bluebird but I am sure both are almost same. So probably problem is promiseItem is not a Q promise it is a mongoose promise for query. If you want to execute that query as Q promise you should use execAsync instead of exec. And you must promisify mongoose with Q
Varmint
@bonbonio
Jun 17 2015 14:50
hmm
the promise itself works
it works the first time anyway, when it creates the data, and im using promises elsewhere
im just wondering if the club object and its _id is not remaining an ObjectID()
i read various things about some operations in mongoose not returning objects with ObjectId's as the ObjectId Type
Erinç Fırtına
@EricMcRay
Jun 17 2015 14:56
I got it.
Varmint
@bonbonio
Jun 17 2015 14:58
oh?
Erinç Fırtına
@EricMcRay
Jun 17 2015 14:58
club is a mongoose object. Try club.toJSON then delete id field and pass that clean obj to update
Varmint
@bonbonio
Jun 17 2015 14:58
ahhhhh
hmm
so i wonder if i should instead just not make club a nongoose object
var club = new Club(clubObj); // becomes
var club = clubObj;
as the new() is what makes it a mongoose object
if i understand it right
the json it loads are plain json objects
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:00
if you dont want to use any model stuff
Varmint
@bonbonio
Jun 17 2015 15:01
if you look at the gist
club is only used to pass into the update
and for a find query
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:01
like virtuals or methods
Varmint
@bonbonio
Jun 17 2015 15:01
so i think this makes sense, and it'll be a few hours until I can confirm
but i think that has helped massively eric
ive been westling with automating content creation for potentially thousands of content items
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:02
for example in my User model I have hashedpassword, salt fields and password as virtual
I use this code to generate hashedpassword and salt
Varmint
@bonbonio
Jun 17 2015 15:02
by virtual what do you mean, sorry
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:03
var user = new User({password: 'testpw'})
Varmint
@bonbonio
Jun 17 2015 15:03
extentions to mongoose inside pre save events?
oh right
i would just like to reiterate my thanks for your input eric :)
hopefully this will work out
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:04
after I create obj from a model I can get hashedpassword like user.hashedpassword
you welcome if you have any other problem we are all here :D
Varmint
@bonbonio
Jun 17 2015 15:08
i should really start to author some of my experience with the stack im building
ive been across so many, many things, libraries with so many niggles and gotchas
Erinç Fırtına
@EricMcRay
Jun 17 2015 15:09
yeah, same :D