These are chat archives for Automattic/mongoose

25th
Jan 2018
Aleš
@fxck
Jan 25 2018 06:27

@lineus exactly,

attempting to put anything else in there will cause a ValidationError from mongoose.

so I'll still have to 1) save foo 2) get their ids 3) save those ids to bar

basically I'm looking for a way to do this in one go, rather than save one then the other
Aleš
@fxck
Jan 25 2018 06:32
well realistically I'm creating objectid string using https://github.com/williamkapke/bson-objectid on frontend, so I could probably do this in one go, but I'm not sure if it's a good practices and it's a little more complicated to deal with optimistic updates when I'll be trying to save nested "entities"
Mohamed Heiba
@heiba
Jan 25 2018 07:41
Hello all
Mongoose 5 suddenly errors “MongoError: unknown string alias for $type: 2dsphere”
Can anybody help out please
Kev
@lineus
Jan 25 2018 09:32
@heiba did you just upgrade to 5? If so, what version where you using before?
Kev
@lineus
Jan 25 2018 10:10
nevermind, I see that in the stackoverflow question. Have you tried opening an issue with mongoose on github? Though, the error is actually coming from the native js driver which is maintained by mongodb, so it might be worth opening a bug with them instead.
JP Lew
@jplew
Jan 25 2018 19:11

Hey all, I have a question about memory consumption. I'm writing a word game with the MEAN stack. I have a list of English words stored in a mLab MongoDB (160k words, ~5MB). In order to rapidly query the data, I want to load the entire word bank in memory whenever a user starts a game. The plan is to create a service that makes a GET request to a Mongoose connection, and receive a 5MB JSON packet.

For testing, I am on a free Heroku plan which caps at 1000 hours a month and has 512MB of RAM. My question is, when I eventually start paying for data, will this approach be costly and inefficient? Will these 5MB packets cumulate and consume tons of data? The alternative to storing the word bank in a variable on the client-side, as far as I see it, is to continually query the DB for data throughout the apps lifecycle, which might affect performance. I'm pretty new to this so any help is appreciated.

Juha Lindstedt
@pakastin
Jan 25 2018 20:53
Just try to query the database instead. MongoDB is super fast and memory-boosted.
JP Lew
@jplew
Jan 25 2018 20:53
ok thanks @pakastin
Juha Lindstedt
@pakastin
Jan 25 2018 20:54
You can also setup an in-memory replica of MongoDB
However that’s premature optimization. WiredTiger is really fast too, just give it a try! 😉
If you’re using Mongoose, remember to use .lean() – that’ll make queries way faster!
And also remember to index!
Mohamed Heiba
@heiba
Jan 25 2018 20:56
@lineus Thanks, I think I will open that issue with Mongoose
Documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments. They have no save method, getters/setters or other Mongoose magic applied.
This is a great option in high-performance read-only scenarios, especially when combined with stream.
You can PM me if you need help or more tips 😉
JP Lew
@jplew
Jan 25 2018 20:58

thanks I'll try the first option then check out the replica if I run into issues. Another question. My collection has this structure:

{
    id: 'a',
    words: [
    'a',
    'aa',
    'aal',
    'aals',
    'aardvark'
  ]
}

I can determine whether a word fragment exists like this: db.words.find( {words: /aard/} ).count(). But I want to get the actual word matched, aardvark. What is the best way to do this, using collection.mapReduce?

@pakastin
JP Lew
@jplew
Jan 25 2018 21:19
also, does it make more sense to restructure my DB? Right now, I have 26 documents, one for each letter of the alphabet. Would it be preferable to split that into 160k separate documents, and index them by the starting letter? Then I don't have to worry about extracting values from an embedded field.
Juha Lindstedt
@pakastin
Jan 25 2018 22:22
Hmm..you shouldn’t do optimizations yourself, but to trust the database
I mean for example that $text uses indexed text
I’m not 100% sure what you’re doing though, but I think you should use the native features of MongoDB
Евгений Новиков
@inqueez_twitter
Jan 25 2018 22:27
Hello everyone! Does mongoose v5 support nested virtuals? After update I ran into the issue with that. It does not populate them (have to use doc.populate("foo.bar").execPopulate()). To JSON also stopped work, it skips nested virtuals. Is it a bug or breaking changes? Thanks.
JP Lew
@jplew
Jan 25 2018 22:27
Right. That's why I was asking about mapReduce(). I don't want to return a list of documents, I want to return a set of values contained in that document. Like for example I want to search aard and for it to return aardvark, not the whole document.
Juha Lindstedt
@pakastin
Jan 25 2018 22:28
You could also put them to a separate collection and reference to paremt
(parent)
Then you could search them separately..
Also notice the 16 MB limit of a single document in a collection
JP Lew
@jplew
Jan 25 2018 22:29
oh, so like a foreign key on the words collection, that connects to the letters collection
re: 16MB limit, that's good to know
Juha Lindstedt
@pakastin
Jan 25 2018 22:33
Yeah
JP Lew
@jplew
Jan 25 2018 22:36
great, thanks for guiding me
I'll try running some tests and seeing for myself what works
Juha Lindstedt
@pakastin
Jan 25 2018 22:41
👍