These are chat archives for Automattic/mongoose

25th
Jan 2016
J
@jhyland87
Jan 25 2016 04:42
@db386 You just need to turn it into a string? As in JSON.stringify() ?
db386
@db386
Jan 25 2016 06:21
That was the hope, and it works just fine with a find query. If I was using a save query it would come back undefined. I ended up finding a solution where it gets added into a JSON obj with a function at the tail end that makes it workable
This was the code:
var data = {
obj: docs,
function(nm) { // arg: object name
//alert(nm); obj
var o = {};
for ( var prop in this ) {
// convert RegExp to string
if ( this[prop] && this[prop].constructor == RegExp ) {
o[prop] = this[prop].toString();
} else if ( prop == 'm' ) { // function pointer
o[prop] = 'fn'; // would be removed by stringify otherwise
} else if ( typeof this[prop] === 'undefined' ) {
o[prop] = 'undefined'; // would be removed by stringify otherwise
} else if ( prop === 'bool' ) {
continue; // don't send to stringify
} else { // to pass value unchanged
o[prop] = this[prop];
}
}
return o; // passed to stringify
}
}
Agli Pançi
@aglipanci
Jan 25 2016 11:37
Hey guys i have created a {strict:false } model which is not working, the model here: http://pastebin.com/DfWKZEv8, then i try to add some properties which are not defiled in the model like here: http://pastebin.com/FPsgqFyM which are not saved in db, any idea?
J
@jhyland87
Jan 25 2016 15:10
@aglipanci Uhm, im new to Mongoose and MDB, but I noticed that too, I figured it was just because you need to use what you've specified in the models schema.. made sense to me..
WHats the point of using the ORM/Schema if you're... not going to use it
Oh @aglipanci , nvm, I hadnt looked at the strict yet
J
@jhyland87
Jan 25 2016 15:29
This message was deleted
J
@jhyland87
Jan 25 2016 15:39
Hey guys, Say you had a one-to-many relationship (person-to-cars, for this example), do you think its best to reference the person ID in each car document? Or should the person document have an array of cars it owns?
Alex Deas
@alex-deas
Jan 25 2016 15:49
Depends on what you want to do with the data, for this example if it's just so that you can see a list of cars that a person owns it would be better to have an array of cars. If you want to do something like compare how many people own a particular car, it would be better to reference the is. The best way is dependent on how you want to use the data
Reference the id *
J
@jhyland87
Jan 25 2016 15:50
Ugh, thats a hard one in this situation... Because there would be a lot working with both the Cars AND the People... Not to mention, this is the backend for an API, so there are uses that I cant account for just yet
Is there a way to automatically do both?
I saw a post on SO where someone recommended doing both, but that immediately brings questions about the possibility of data getting out of sync
@Ingui-Frea
J
@jhyland87
Jan 25 2016 16:24

Is there a reason that catch() wont work on the Promise returned by the models save()?

    partition.save()
        .then( partition => {
            console.log('Added field!')
            done()
        })
        .catch( err => {
            console.error('ERROR while saving partition:', err)
            done()
        })

And the error I get is:

/Users/jhyland/Documents/scripts/js/node/test/mongodb/my_file.js:47
    }).catch(function (err) {
            ^

TypeError: partition.save(...).then(...).catch is not a function
And actually, just realized that Mongoose uses Blubird, so heres the new (still not working) snippet:
    partition.save()
        .then( partition => {
            console.log('Added field!')
        })
        .catch( err => {
            console.error('ERROR while saving partition:', err)
        })
        .finally( () => {
            done()
        })
J
@jhyland87
Jan 25 2016 16:31
Alex Deas
@alex-deas
Jan 25 2016 17:10
@jhyland87 I can't say if there's a way of doing both. I'm sure there is but it seems like it'd be a problem to maintain. If you want to delete one record you'd need to ensure it's deleted from both documents. To be safe it'd probably be best to store them in seperate documents with an id; both would work just fine for both cases I mentioned but the first would only have the benefit of performance gains in a few cases, so to increase scalability I'm siding towards seperate documents
J
@jhyland87
Jan 25 2016 17:12

@Ingui-Frea

To be safe it'd probably be best to store them in seperate documents with an id
So you think having a 3rd collection with documents to associate the documents in the other two tables? (Kinda like a "pivot" or "association" table in MySQL?)

Thats what I was switching to MongoDB to get away from. hmmm
I like the idea of having an array of stories in the Person documents... but then what if I wanted to write a script that would just simply let you provide a story ID, and then tell you who the Owner is (as in what Person document)
is there a way to do something like findOne() on the people collection, and return the row that has a specific ID in the _stories array? (Kinda like an inArray )
Tim Elfelt
@timelf123
Jan 25 2016 17:16
something like find Person where {'owner': new ObjectId('idhere')}
J
@jhyland87
Jan 25 2016 17:17
wait... what? I mean if the Person documents have an array of stories they have, can you do an inArray kinda search within a query.
Alex Deas
@alex-deas
Jan 25 2016 17:18
Would the _stories be unique to each Person?
J
@jhyland87
Jan 25 2016 17:18
Well actually, since Im using the People -> Cars example, the Person would have an array of Car ID's
yes
the car can only belong to one person
and the person can have many cars
one-to-many
In MySQL I would hold the person ID in the cars table, but I guess in MDB its best to store an array of the car ID's in the person document?
(Btw, this example is a very scaled down analogy, and for this situation, sub-documents wont be used)
Alex Deas
@alex-deas
Jan 25 2016 17:20
Ah sorry, I was thinking in a many to many context. Yes, have them as children of Person. Yes an array would be better in that case
J
@jhyland87
Jan 25 2016 17:21
@Ingui-Frea Ok, so if I wanted to find out What Person owned which Car, with just the car ID, whats the quickest way? Is there a query to do an in_array on the Person._cars ?
Thats the real hangup im having here.. is how to do a quick association in the other direction, meaning having just the car ID
J
@jhyland87
Jan 25 2016 17:27
Hmmm... not sure.. lol

Just found this:

User.find()
  .where('fb.id')
  .in([3225, 623423, 6645345])
  .exec(function (err, records) {
    //make magic happen
  });

Is it possible to do the opposite? something like this:

User.find()
  .where('56a64145f6118c2b2b0dc010') // Looking for this in the cars array
  .in('fb.cars') // Array item in document
  .exec(function (err, records) {
    //make magic happen
  });
@Ingui-Frea the link you sent uses a collection to manage the categories, since its multi-leveled. This is just one level.. Cars and their owner
J
@jhyland87
Jan 25 2016 17:32
@Ingui-Frea This is more accurate. But it doesnt show an example of how you can find the owner of a book, with just teh book ID

And that document also says that thats not a good idea to store it like that..

To avoid mutable, growing arrays, store the publisher reference inside the book document:

I guess its also noteworthy to say that in the real app, (keeping this analogy), the person can own hundreds of thousands of cars..
Alex Deas
@alex-deas
Jan 25 2016 17:35
Hmm...maybe I'm going too simplistic but you should be able to just use .find()
db.Person.find({}: {"Cars": [CAR_ID]});
With changes depending on how you're structuring the document