These are chat archives for Automattic/mongoose

27th
Dec 2017
Jacob Hoke
@threads13
Dec 27 2017 04:37
This message was deleted
Jacob Hoke
@threads13
Dec 27 2017 04:46
Does anyone see anything wrong with my schema's here? I can get both collectins to save to my DB, but I cannot get he comment data associated with the blog data.
var mongoose = require("mongoose");

var blogSchema = new mongoose.Schema({
    author: String,
    title: String,
    desc: String,
    posted: {type: Date, default: Date.now()},
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }
    ] 
});

module.exports = mongoose.model("Blog", blogSchema);
var mongoose = require("mongoose");

var commentSchema = new mongoose.Schema({
    author: String,
    desc: String,
    posted: {type: Date, default: Date.now()} 
});

module.exports = mongoose.model("Comment", commentSchema);
Nick Karnik
@theoutlander
Dec 27 2017 06:12
@threads13 are you providing the comment id to blog schema? How are you doing the saves ?
Jacob Hoke
@threads13
Dec 27 2017 06:29
Comment.create(req.body.comment, function(err, comments){
                if(err){
                    console.log(err);
                } else {
                    blog.comments.push(comments);
                    blog.save();
                    console.log(blog);
                    res.redirect("/blogs/" + blog._id);
                }
            });
Nick Karnik
@theoutlander
Dec 27 2017 06:30
your schema is defining a reference to an array of comments by id
so you can’t store the entire object
you can only push the id
when you query blogschema, you can use populate to retrieve the comment
if you want to
Jacob Hoke
@threads13
Dec 27 2017 06:47
ok, I think I see it now. :) TY. I will play around with it a bit.
Nick Karnik
@theoutlander
Dec 27 2017 06:48
:+1:
sergeyzorg
@sergeyzorg
Dec 27 2017 09:33
Hello, can you help me with searching in database mongo of full text?
Nick Karnik
@theoutlander
Dec 27 2017 15:09
@sergeyzorg have you checked the mongo docs?
sergeyzorg
@sergeyzorg
Dec 27 2017 19:38
I have big collection (more than 150.000 records). http://joxi.ru/gmvzqQdUxDoK4m
Create index db.books.getIndexes().find(it=>it.name==="name_text")
and when I create query with 2 or more words, I have error http://joxi.ru/DmBqN3afNlLp0r
Nick Karnik
@theoutlander
Dec 27 2017 22:07
Not sure. What is the error? Is it after a timeout?
Maybe something with a setting on MongoDB.
Nick Karnik
@theoutlander
Dec 27 2017 23:28
formatting issue :D
Jacob Hoke
@threads13
Dec 27 2017 23:29
app.post("/blogs/:id/comments", function(req, res){
    Blog.findById(req.params.id, function(err, blog){
        if(err){
            // console.log(err);
            res.redirect("/blogs");
        } else {
            Comment.create(req.body.comment, function(err, comments){
                if(err){
                    console.log(err);
                } else {
                    console.log(comments._id);
                    blog.comments.push(comments._id);
                    blog.save();
                    console.log(blog);
                    res.redirect("/blogs/" + blog._id);
                }
            });
        }
    });
});
That's better. :)
Nick Karnik
@theoutlander
Dec 27 2017 23:29
what is blog ?
oh
it’s passed in
does it find the blog ?
does it create the comment ?
Jacob Hoke
@threads13
Dec 27 2017 23:30
Comment it created and I see it in mongo
Nick Karnik
@theoutlander
Dec 27 2017 23:30
there’s also another easier way to do this … you can generate that id yourself and pass it into comment and blog. But, that’s just an FYI.
Jacob Hoke
@threads13
Dec 27 2017 23:30
but the id's aren't getting pushed
Nick Karnik
@theoutlander
Dec 27 2017 23:30
is there an error on the comment.create callback ?
is it printing comments._id ?
@threads13 zzzzz
Jacob Hoke
@threads13
Dec 27 2017 23:33
i have an error popping up when I run the server, but i don't believe that it is related
it does print the id's in the console, yes
Nick Karnik
@theoutlander
Dec 27 2017 23:33
ok … then show me your schema again
i guess it’s up there ...
Jacob Hoke
@threads13
Dec 27 2017 23:34
sure thing
```
argh lol
var mongoose = require("mongoose");

var blogSchema = new mongoose.Schema({
    author: String,
    title: String,
    desc: String,
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }
    ]
});

module.exports = mongoose.model("Blog", blogSchema);
var mongoose = require("mongoose");

// something needs to be rearranged in my comments to make the schemas match
var commentSchema = mongoose.Schema({
    desc: String,
    author: String,

});

module.exports = mongoose.model("Comment", commentSchema);
try this first
Create an id
and assign to both comment and push to blog comments
and see if it shows up there
when you first create it ….
Jacob Hoke
@threads13
Dec 27 2017 23:36
ok, let me see
Nick Karnik
@theoutlander
Dec 27 2017 23:37
Do you know how to create the id ?
Jacob Hoke
@threads13
Dec 27 2017 23:37
i haven't done it manually before, no
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
also, you can try changing it from an array to just an object with one id
and see if that works
there’s probably a minor issue somewhere here
Jacob Hoke
@threads13
Dec 27 2017 23:40
oh ok. I see. I will take a look at doing this and let ya know what I find :)