These are chat archives for Automattic/mongoose

3rd
Nov 2018
Kev
@lineus
Nov 03 2018 09:49
@mbellemo_twitter can you connect to your db with the mongo shell and see what indexes exist for that collection, making sure that the collection matches the one from your app? db.collection.getIndexes()
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:12
Screen Shot 2018-11-03 at 12.12.35.png
@lineus I use Robomongo... it seems that I have a single index on the _id field of the collection... I guess it is done automatically by MongoDB
Kev
@lineus
Nov 03 2018 11:23
here's the output of creating the text index and printing it out ( along with inserting a doc in the collection from the mongo shell:
mongoose>: mongo --quiet
> use helios
switched to db helios
> db.createCollection('feedbacks')
{ "ok" : 1 }
> db.feedbacks.createIndex({ title: 'text', body: 'text' })
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.feedbacks.insertOne({ title: 'test', body: 'something' })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5bdd84904c03d4b95ba3fa6a")
}
> db.feedbacks.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "helios.feedbacks"
        },
        {
                "v" : 2,
                "key" : {
                        "_fts" : "text",
                        "_ftsx" : 1
                },
                "name" : "title_text_body_text",
                "ns" : "helios.feedbacks",
                "weights" : {
                        "body" : 1,
                        "title" : 1
                },
                "default_language" : "english",
                "language_override" : "language",
                "textIndexVersion" : 3
        }
]
>
mongoose>:
your robomongo output doesn't appear to show any existing text indexes
@mbellemo_twitter are you destroying the collection and recreating it for testing each time you start your app?
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:26
I wonder if I am doing something wrong in the model then...
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
const validator = require('validator');

var UpvoteSchema = new Schema({
    comments: {
        type: String,
        trim: true
    },
    priority: {
        type: String,
        required: true,
        enum: ['must', 'should', 'could'],
        trim: true
    },
    owner_id: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    customer_id: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Customer'
    }
});

var FeedbackSchema = new Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    description: {
        type: String,
        trim: true
    },
    upvotes: [UpvoteSchema],
    company_id: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Company'
    },
    project_id: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Project'
    },
    owner_id: {
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    size: {
        type: Number,
        default: 0
    },
    potential: {
        type: Number,
        default: 0
    },
    real: {
        type: Number,
        default: 0
    },
    updatedAt: {
        type: Date
    },
});

FeedbackSchema.index({ title: 'text', description: 'text' });
var Feedback = mongoose.model('Feedback', FeedbackSchema);

module.exports = {
    Feedback
}
I can dump the DB
and re-start the node server
Kev
@lineus
Nov 03 2018 11:27
I wasn't suggesting that's what you should do, just curious.
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:29
Screen Shot 2018-11-03 at 12.28.16.png
but it was a good suggestion, it seems :)
Kev
@lineus
Nov 03 2018 11:29
oh, hehe. Cool!
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:30
@lineus I mean the endpoint does not retrieve me anything back... which means that the $search does not find anything which is strange, but at least i do not get the error message
I am printing the query filter
{ company_id: 5bdd8616c894f01770059868,
  project_id: '5bdd861ec894f0177005986c',
  '$text': { '$search': 'feed' } }
is it right? does it perform non-exact search like this, or i have to use a regexp?
all the objects that I create are called Feedback something...
I thought that the query would find similar matches but it does not
Kev
@lineus
Nov 03 2018 11:34
how many docs are currently in that collection?
can you share the doc that should match that query?
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:37
{
    "feedbacks": [
        {
            "size": 0,
            "potential": 0,
            "real": 0,
            "_id": "5bdd8623c894f0177005986d",
            "title": "Feedback 5bdd861ec894f0177005986c",
            "owner_id": "5bdd8616c894f01770059869",
            "company_id": "5bdd8616c894f01770059868",
            "project_id": "5bdd861ec894f0177005986c",
            "upvotes": [],
            "__v": 0
        },
        {
            "size": 0,
            "potential": 0,
            "real": 0,
            "_id": "5bdd8624c894f0177005986e",
            "title": "Feedback 5bdd861ec894f0177005986c",
            "owner_id": "5bdd8616c894f01770059869",
            "company_id": "5bdd8616c894f01770059868",
            "project_id": "5bdd861ec894f0177005986c",
            "upvotes": [],
            "__v": 0
        },
        {
            "size": 0,
            "potential": 0,
            "real": 0,
            "_id": "5bdd8625c894f0177005986f",
            "title": "Feedback 5bdd861ec894f0177005986c",
            "owner_id": "5bdd8616c894f01770059869",
            "company_id": "5bdd8616c894f01770059868",
            "project_id": "5bdd861ec894f0177005986c",
            "upvotes": [],
            "__v": 0
        }
    ],
    "current": 1,
    "pages": 1,
    "total": 3
}
Kev
@lineus
Nov 03 2018 11:41
did you try '$text': { '$search': 'Feed' } ?
or '$text': { '$search': 'Feedback' } or '$text': { '$search': 'feedback' } ?
I don't have a lot of meaningful experience with searching text indexes. I'll look at the docs for how the matching works by default.
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 11:42
Feedback works
Feed does not work
do i have to use a regexp maybe?
Kev
@lineus
Nov 03 2018 11:52
no, by default text search queries are not case sensitive and searches that are not case sensitive match on whole words only. If you want to be able to search on partial words, you have explicitly set $caseSensitive: true. The docs are here
$text: { $search: 'Feed', $caseSensitive: true } might work
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 12:04
it still does not unfortunately
Kev
@lineus
Nov 03 2018 12:32
yeah, I was just playing around with it, I can't get it to match on partial words, even with default_language set to 'none' and both $caseSensitive and $diacriticSensitive turned on. Oh well, at least we learned some new things :)
Maurizio Bellemo
@mbellemo_twitter
Nov 03 2018 12:37
strange.... it seems i have to go back to the initial method with regexp
i wonder what would be the use of text then...
it search only complete words