These are chat archives for Automattic/mongoose

8th
Jun 2017
Thomas Havlik
@thavlik
Jun 08 2017 01:22
Hey folks, is there any reason why I can verify db.users is populated using mongo but isn't populated from node? const User = mongoose.model("user", UserSchema); User.findOne({}) results in a null document?
Does running mongo test --eval "db.dropDatabase()" && mongorestore ./test/db after I create the database but before I query it do something that would cause this?
I'm running that command prior to my unit tests to restore the database. Should I use mongoose instead of CLI?
Thomas Havlik
@thavlik
Jun 08 2017 01:28
Hmm it shouldn't.
Crap it does
Thomas Havlik
@thavlik
Jun 08 2017 02:25
ahhh yup it was that mongo test --eval command. Got rid of it and just used mongorestore --drop ./test/db
Pasi Hirvonen
@pasih
Jun 08 2017 08:31
any good suggestions for debugging why .exec() hangs? Mongo logs (with -vvvv) show no request so I don't think it gets there. Mongoose debug isn't showing anything either. I get back a promise but it doesn't seem to ever be resolved.
strangely, this code worked previously. Unfortunately this is a quick mocker thingy I made so it's not under version control so I can't see if the library versions have changed (they match another code base that works though)
also I'm seeing the mpromise deprecation warning despite setting mongoose.Promise to global.Promise at startup
Joshua Austill
@jlaustill
Jun 08 2017 12:43
@pasih If you replace your mongo call with a factory that returns a resolved promise does it finish?
Дмитрий
@CatBegemot
Jun 08 2017 16:33

How filter object who have parent - sa in mongoose

> db.items.find( { $text: { $search: "Item" } });
{ "_id" : ObjectId("593976c281683f6478136a44"), "name" : "Second Item af" }
{ "_id" : ObjectId("5939671181683f6478136a3d"), "name" : "Fisrt Item" }
{ "_id" : ObjectId("593976bb81683f6478136a42"), "name" : "Second Items" }
{ "_id" : ObjectId("593976be81683f6478136a43"), "name" : "Second Item" }
{ "_id" : ObjectId("5939790381683f6478136a47"), "name" : "Item", "parent" : "sa" }
>

It's work

> db.items.find( { parent : 'sa', $text: { $search: "Item"}});
{ "_id" : ObjectId("5939790381683f6478136a47"), "name" : "Item", "parent" : "sa" }

Not Work

app.post("/search", (req, res) => {
    let parent =  {parent: 'sa'};
    console.log(parent);
    Items.find({parent, "$text": {"$search": req.body.searchText}}, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
XHMM
@XHMM
Jun 08 2017 16:39
This message was deleted
This message was deleted
Дмитрий
@CatBegemot
Jun 08 2017 16:42
@XHMM not work too
Дмитрий
@CatBegemot
Jun 08 2017 16:50
who know?
Joshua Austill
@jlaustill
Jun 08 2017 16:58
@CatBegemot "parent": {$ne: "sa" }
@CatBegemot try something like this
app.post("/search", (req, res) => {
   let query = {};
    query["parent"] =  'sa';
    query["$text"] = {"$search": req.body.searchText};
    console.log(parent);
    Items.find(query, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
Дмитрий
@CatBegemot
Jun 08 2017 17:03
@jlaustill no
Joshua Austill
@jlaustill
Jun 08 2017 17:04
no what? lol
Дмитрий
@CatBegemot
Jun 08 2017 17:04
@jlaustill no work, no find
Joshua Austill
@jlaustill
Jun 08 2017 17:05
what does console.log(query); give you?
Дмитрий
@CatBegemot
Jun 08 2017 17:06
{ parent: 'sa', '$text': { '$search': 'Item' } }
undefined
app.post("/search", (req, res) => {
   let query = {};
    query["parent"] =  'sa';
    query["$text"] = {"$search": req.body.searchText};
    console.log(query);
    Items.find(query, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
Joshua Austill
@jlaustill
Jun 08 2017 17:07
it's exact same query as above
Дмитрий
@CatBegemot
Jun 08 2017 17:07
@jlaustill yes
Joshua Austill
@jlaustill
Jun 08 2017 17:08
I would check your definition of Items, maybe it's pointing to the wrong db or collection
Дмитрий
@CatBegemot
Jun 08 2017 17:08
@jlaustill all object find
app.post("/search", (req, res) => {
    let parent =  {parent: 'sa'};
    console.log(parent);
      Items.find({ "$text": {"$search": req.body.searchText}}, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
@jlaustill but i want find object only with parent
Joshua Austill
@jlaustill
Jun 08 2017 17:12
@CatBegemot if it works in the cli but not your code with the exact same query, something is wrong with your code, that's pretty simple.
Дмитрий
@CatBegemot
Jun 08 2017 17:14
@jlaustill Therefore, I wrote here and asked for help
Joshua Austill
@jlaustill
Jun 08 2017 17:14
:)
how are you declaring your Items collection, show that code
Дмитрий
@CatBegemot
Jun 08 2017 17:16

all object find in code, but i want find object with parent.

let Items = require('./models/items.js');

U want see model?

Server work on 3000 port
Connected MongoDB
[ { _id: 593976c281683f6478136a44, name: 'Second Item af' },
  { _id: 5939671181683f6478136a3d, name: 'Fisrt Item' },
  { _id: 593976bb81683f6478136a42, name: 'Second Items' },
  { _id: 593976be81683f6478136a43, name: 'Second Item' },
  { _id: 5939790381683f6478136a47, name: 'Item' } ]
app.post("/search", (req, res) => {
    // let parent =  {parent: 'sa'};
    // console.log(parent);
      Items.find({ "$text": {"$search": req.body.searchText}}, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
why not write in console parent parametr?
> db.items.find();
{ "_id" : ObjectId("5939671181683f6478136a3d"), "name" : "Fisrt Item" }
{ "_id" : ObjectId("593976bb81683f6478136a42"), "name" : "Second Items" }
{ "_id" : ObjectId("593976be81683f6478136a43"), "name" : "Second Item" }
{ "_id" : ObjectId("593976c281683f6478136a44"), "name" : "Second Item af" }
{ "_id" : ObjectId("5939770981683f6478136a45"), "name" : "safaffasfa" }
{ "_id" : ObjectId("5939781d81683f6478136a46"), "name" : "safaffasfa", "parent" : "sa" }
{ "_id" : ObjectId("5939790381683f6478136a47"), "name" : "Item", "parent" : "sa" }
>
@jlaustill have idea?
Joshua Austill
@jlaustill
Jun 08 2017 17:21
I don't understand your question?
Дмитрий
@CatBegemot
Jun 08 2017 17:23

@jlaustill in shell

> db.items.find();
{ "_id" : ObjectId("5939671181683f6478136a3d"), "name" : "Fisrt Item" }
{ "_id" : ObjectId("593976bb81683f6478136a42"), "name" : "Second Items" }
{ "_id" : ObjectId("593976be81683f6478136a43"), "name" : "Second Item" }
{ "_id" : ObjectId("593976c281683f6478136a44"), "name" : "Second Item af" }
{ "_id" : ObjectId("5939770981683f6478136a45"), "name" : "safaffasfa" }
{ "_id" : ObjectId("5939781d81683f6478136a46"), "name" : "safaffasfa", "parent" : "sa" }
{ "_id" : ObjectId("5939790381683f6478136a47"), "name" : "Item", "parent" : "sa" }
>
> db.items.find( { parent : 'sa', $text: { $search: "Item"}});
{ "_id" : ObjectId("5939790381683f6478136a47"), "name" : "Item", "parent" : "sa" }
>

I see object with all parametrs now, but if run code

app.post("/search", (req, res) => {
    // let parent =  {parent: 'sa'};
    // console.log(parent);
      Items.find({ "$text": {"$search": req.body.searchText}}, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});

i see in console

[ { _id: 593976c281683f6478136a44, name: 'Second Item af' },
  { _id: 5939671181683f6478136a3d, name: 'Fisrt Item' },
  { _id: 593976bb81683f6478136a42, name: 'Second Items' },
  { _id: 593976be81683f6478136a43, name: 'Second Item' },
  { _id: 5939790381683f6478136a47, name: 'Item' } ]
why not see all parametrs now?
and why not
app.post("/search", (req, res) => {
    // let parent =  {parent: 'sa'};
    // console.log(parent);
      Items.find({parent: 'sa', "$text": {"$search": req.body.searchText}}, (err, fb) => {
          console.log(fb);
          res.render('folder', {
              users: fb
          });
      });
});
Дмитрий
@CatBegemot
Jun 08 2017 17:29
@jlaustill understand?
Joshua Austill
@jlaustill
Jun 08 2017 17:50
Your Item model defines them?
Дмитрий
@CatBegemot
Jun 08 2017 19:08
no)))
Joshua Austill
@jlaustill
Jun 08 2017 19:10
then they will be ignored :)
Дмитрий
@CatBegemot
Jun 08 2017 19:11
i incorrect read

what u mean

Your Item model defines them?

Joshua Austill
@jlaustill
Jun 08 2017 19:13
can you show your Item model?
Дмитрий
@CatBegemot
Jun 08 2017 19:28
let mongoose = require('mongoose');
let tree = require('mongoose-path-tree');

// Folder Schema
let itemShema = mongoose.Schema({
    name: {
        type: String,
        required: true
    }
});

itemShema.plugin(tree);

let Item = module.exports = mongoose.model('Items', itemShema);
Joshua Austill
@jlaustill
Jun 08 2017 19:30
try
// Folder Schema
let itemShema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    parent: {
        type: String
    }
});
Дмитрий
@CatBegemot
Jun 08 2017 19:32
nothing
Joshua Austill
@jlaustill
Jun 08 2017 19:32
even when you query for everything?
Дмитрий
@CatBegemot
Jun 08 2017 19:34
no, when i query all, without parent i see all objects in browser, but in console
Connected MongoDB
[ { _id: 593976c281683f6478136a44, name: 'Second Item af' },
  { _id: 5939671181683f6478136a3d, name: 'Fisrt Item' },
  { _id: 593976bb81683f6478136a42, name: 'Second Items' },
  { _id: 593976be81683f6478136a43, name: 'Second Item' },
  { _id: 5939790381683f6478136a47, name: 'Item' } ]
and i not know
maybe i must do it? db.items.createIndex({"name":"text","patern":"text"})
Joshua Austill
@jlaustill
Jun 08 2017 19:38
That should only affect your performance, not your results
Дмитрий
@CatBegemot
Jun 08 2017 19:39
then i not know
Дмитрий
@CatBegemot
Jun 08 2017 22:05
who can help me?