These are chat archives for Automattic/mongoose

16th
Feb 2016
antonioaltamura
@antonioaltamura
Feb 16 2016 00:52

guys I always though I can post a json query and simply as that get the results.
Now I'm seeing there is no easy way to join collections. I'm a little worried about my project. I repeat the schemas I posted above

var com_user_schema = new Schema({
    name: { type: String, required: true },
    age: {type:Number, required:true}
});

var com_user= mongoose.model('com_user', com_user_schema);

var reg=new Schema({number: Number, year: Number});
var com_post_schema = new Schema({
    content: { type: String, required: true },
    postedBy: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'com_user'
    }]
});
var com_post= mongoose.model('com_post', com_post_schema);

My goal should be the client thats sends a json in a form similar to:

{content: "contains this",
postedBy: {name:"John"}}

and I get the John posts that have content that contains "contains this". Regexp apart, this looks pretty tough to me currently (obiouvsly the json query isn't that simple, it's dynamic and it could be huge, the documents have a lot of searchable fields, and refs should be searchable NOT only by _id).
I really need an expert advice to make the right choices to accomplish that..

neon69
@sidorenko69
Feb 16 2016 07:37
Hi. I have one problem: i cant use any crud operations for multiple connections for different hosts.
Always all data puts to the 1st mongoose model.
please explain:how can i use another connection?
Konstantin Baierer
@kba
Feb 16 2016 11:34
@jhyland87 createdBy/modifiedBy sounds like a job for a plugin.
Chris Paradeise
@cparadeise
Feb 16 2016 12:29
If I modify schemas, is it considered safe to just run a mapreduce function on the data to transform it appropriately? or is there an easier way?
Alex
@whitecolor
Feb 16 2016 15:02
How to correctly call this thing that is returned by Model.update(...) or Model.create(...), to wich then I can apply .exec(), is it called query?
Alvaro Almendros Gala
@arkdelkaos
Feb 16 2016 15:07

Sorry to bother you, but I'm kind of desperate (I'm in a hurry to push to production), and any clue about my error will be awesome :):
I've an schema like this (simplified):

var TorneoSchema = new mongoose.Schema({
   owner: {
    userId: {
      type: mongoose.Schema.Types.ObjectId, ref: 'User'
    },
    name: String
  },
  users: [{
    userId: {
      type: mongoose.Schema.Types.ObjectId, ref: 'User'
    },
    name: String
  }],
  chat: [{
    date: {
      type: Date, default: Date.now
    },
    msg: String,
    nick: String,
    userId: {
      type: mongoose.Schema.Types.ObjectId, ref: 'User'
    }
  }]
});

The thing is that when I put new mesages on the chat (previously pushed into the torneos $resourcewith currentTorneo.chat.push(newMsg), and then $update, I send to my backed api the tournament, and everything looks right on req.body.
...But when I update the tournament with the 'updated one', my db changes all the chat documents and the users documents to copies of the same first document on the array. If I have, lets say 10 subdocs on users, it makes 10 copies of the first document of te original array.
I don't even know where to start looking :? I know is backend related, because the requestis a valid document...but I'm completely lost...

Konstantin Baierer
@kba
Feb 16 2016 16:06
@alfredopacino Complex multi-join normalized data? Might not be the best use case for a document DB. You have to create complexity somewhere: Either make querying more complex (essentially doing joins yourself) or duplicate/denormalize if necessary.
@sidorenko69 I guess multiple connections to multiple hosts is better handled on the mongodb side (shards, master/slave etc)
J
@jhyland87
Feb 16 2016 16:08
@kba yep, actually created a plugin for it. Question though. From within the plugin... how can I tell if the document is being created, or modified?..
Konstantin Baierer
@kba
Feb 16 2016 16:09
isModified()?
?..
I mean how can I tell if its being created for the first time?
Konstantin Baierer
@kba
Feb 16 2016 16:09
think that's a document's method, isModifieed
J
@jhyland87
Feb 16 2016 16:09
hm..
so that will be false if its created for the first time?
ok
Konstantin Baierer
@kba
Feb 16 2016 16:10
not sure, just guessing, haven't checked
J
@jhyland87
Feb 16 2016 16:10
Ok, diff question, is it possible to disable the updatedAt timestamp? just keep the createdAt
ok
Konstantin Baierer
@kba
Feb 16 2016 16:10
depends on which plugin you're using.
J
@jhyland87
Feb 16 2016 16:10
I was thinking I could check if the createdAt timestamp or createdBy user is populated, if so, its being modified
Konstantin Baierer
@kba
Feb 16 2016 16:10
You have to check the interface of the plugin you're using. Technically, sure it is.
J
@jhyland87
Feb 16 2016 16:10
http://pastebin.com/HN2pNQx4 thats the plugin thus far
The plugin im using? Im creating it :P
im confused, sorry. heh
Automattic/mongoose#2162, should help with checking if its new or old
Konstantin Baierer
@kba
Feb 16 2016 16:13
think i just check whether _id is set or not. probably very bad btu works so far.
J
@jhyland87
Feb 16 2016 16:13
.. Good point actually
lol
well actually... since this is for post, thats not right
err, sorry, this plugin is for pre, not post
I have a diff plugin im creating to create revisions, that works with post
@kba do you know how to disable one timestamp, but not the other? (Disabling updatedAt). I tried:
        timestamps: {
            createdAt: 'createdAt',
            updatedAt: null // Tried: null, undefined, false
        }
Konstantin Baierer
@kba
Feb 16 2016 16:31
@jhyland87 ahhh, i never realized timestamps were part of core mongoose :D i suppose they have been for a while, i just didn't notice. been using plugin for that.
J
@jhyland87
Feb 16 2016 16:31
hahaha
Yeah, they are. I have a collection for revisions, which can only be created and deleted, never updated, so I dont want/need the updatedAt
Konstantin Baierer
@kba
Feb 16 2016 16:33
as for updatedAt: obviously no idea. creating Date()is fairly cheap tho. so maybe just set it to something which won't cause conflicts. or dig in the code to find how it's done
J
@jhyland87
Feb 16 2016 16:42
this should suffice for the (updated|created)By plugin. woot. I love Mongoose, so easy to use
After I realized how easy it is to create plugins, I wrote one to create revisions for documents in a specific container
Im sure theres still a few things i may be doing incorrectly, but this works like a charm