These are chat archives for Automattic/mongoose

9th
Jun 2017
XHMM
@XHMM
Jun 09 2017 03:11
@CatBegemot hi,Ifinally worked it out
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var itemSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    parent: {
        type: String
    }
});
itemSchema.index({name:"text"});
var Item = mongoose.model("Item",itemSchema)
mongoose.connect('mongodb://localhost:27017/Test_DB', function(error){
    if(error) console.log('Failed to connect');
    else {
        Item.find({"$text":{"$search":"Item"}}).exec(function(err,docs){
            console.log(docs)
        })
    }
});
this is my code
the result is below:
[ { _id: 593a0e954410fa0607a6ca2c, __v: 0, name: 'Second Item af' },
  { _id: 593a0e954410fa0607a6ca29, __v: 0, name: 'Fisrt Item' },
  { _id: 593a0e954410fa0607a6ca2a, __v: 0, name: 'Second Items' },
  { _id: 593a0e954410fa0607a6ca2b, __v: 0, name: 'Second Item' },
  { _id: 593a0e954410fa0607a6ca2f,  __v: 0,name: 'Item',parent: 'sa' } ]
XHMM
@XHMM
Jun 09 2017 04:54
hi ,i encountered a werid problem:
This is my code
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
    _id:String,
    name: String,
    jobs:[{type:String,ref:"Job"}]
});
var User = mongoose.model('User',userSchema);

var jobSchema = new Schema({
    _id:String,
   user:{ type: String, ref: 'User' },
   name: String
})
var Job= mongoose.model('Job',jobSchema)

mongoose.connect('mongodb://localhost:27017/Test_DB', function(error){
    if(error) console.log('Failed to connect');
    else {
        User.find().populate({
            path:"jobs"    
        }).exec(function(err,docs){
            console.log(docs)
        })
    }
});
This is my database:
jobs collection:
{
    "_id" : "job1",
    "user" : "user1",
    "name" : "front"
}
{
    "_id" : "job2",
    "user" : "user1",
    "name" : "java"
}
users collection:
{
    "_id" : "user1",
    "name" : "mike",
    "jobs" : [ 
        "job1"
    ]
}
I want to populate the jobs field ,but the result is not right ,i don't konw where is wrong
This is the console: [ { _id: 'user1', name: 'mike', jobs: [ [Object] ] } ]
kailash k yogeshwar
@kailashyogeshwar85
Jun 09 2017 06:44
@XHMM
var userSchema = new Schema({
    _id:String,
    name: String,
    jobs:[{type:String,ref:"Job"}]// this is incorrent should be 
   jobs: [{type: Schema.Types.ObjectId, ref: 'Job' }]
});

//query
User.find(query).populate('jobs')
 .then(docs => { 
     // your document
}).catch(err => err)
XHMM
@XHMM
Jun 09 2017 08:18
.Note: ObjectId, Number, String, and Buffer are valid for use as refs. This is what doc says.
@kailashyogeshwar85
XHMM
@XHMM
Jun 09 2017 08:27
So i think my write is not wrong ?or, it's a bug ?or , something special i didn't considere ?😳
Pasi Hirvonen
@pasih
Jun 09 2017 14:16
@jlaustill yeah. A bit late but yes
Joshua Austill
@jlaustill
Jun 09 2017 14:19
@pasih What was the question again? I've been super busy on here and had a ton of convo's lately :)
Pasi Hirvonen
@pasih
Jun 09 2017 14:22
heh. My mongo queries (in one single mocker app) hang. I've tested my promises, they seem to work just fine (using native promises).
the query doesn't seem to reach Mongo (I turned on verbose logging and tested that I see queries performed elsewhere)
Joshua Austill
@jlaustill
Jun 09 2017 14:23
@pasih I've had this issue as well doing bulk upserts, except with the native driver
essentially, this code prints everything you expect it to print up to resolving the promise. Query? shows the query object returned by .find(), foo.exec() returns a promise and prints "Promise { <pending> }", as expected
but .then() never gets called (nor .catch)
Joshua Austill
@jlaustill
Jun 09 2017 14:26
That is the exact behavior I'm experiencing, except mine is random as all get out
it's weird that you are getting it with mongoose too
I wonder if mongoose and the native driver share an underlying library causing the issue?
Pasi Hirvonen
@pasih
Jun 09 2017 14:28
coooould be I guess
I'm on node-mongodb-native 2.2.26 and mongoose 4.9.8
Joshua Austill
@jlaustill
Jun 09 2017 14:29
2.2.26 here as well
Pasi Hirvonen
@pasih
Jun 09 2017 14:29
the strange is, I have another part of the application that works just fine and is using the exact same versions
(and I even link the schema/model definitions from there)
I should probably test this without Mongoose, this might have absolutely nothing to do with Mongoose
Joshua Austill
@jlaustill
Jun 09 2017 14:30
I think you should too
we've been tracking this if you wanna see what others have ran into
Pasi Hirvonen
@pasih
Jun 09 2017 14:33
MongoClient works
Ankur sharma
@ankur1163
Jun 09 2017 19:12
I want to learn more about populate in mongoose
can someone point me to easy example. I looked at official docs
and those certainly helped me
I have better understanding of populate
but, i want to dig deeper
I also read its not advisale to use arrays as it might face performance issues
so that means we need lot of objects
we have post and user as modals
var UserSchema = {
  _id: String,
  username: String
};

var PostSchema = {
  _id: String,
  user: {
    ref: 'User',
    type: String
  }
};
if we use this Post.find({}).populate('user').exec(callback)
why the result is
v
var results = [
  {
    __t: 'Post',
    _id: '1234',
    user: {
      __t: 'User',
      _id: '5678',
      username: 'josh'
    }
  }
];
i am confused about __t
I assume when one modal is connected to other modal
it uses __t
If that is the case
why did it add __t :"Post"