These are chat archives for Automattic/mongoose

15th
May 2018
Arun Gadag
@arun-awnics
May 15 2018 04:22
@ehsansh Glad to help :smile:
Arye Shalev
@pantchox
May 15 2018 11:37
hi! i have few esp chips, any cool ideas what to do with them?? :)
opes sorry wrong channel lol
Arun Gadag
@arun-awnics
May 15 2018 11:38
@pantchox :smile:
Irfan Raza
@irfaan008
May 15 2018 12:22

Hi All,

I am having views on Mongodb which I would like to access through mongoose but don't see any documentation for the same. Is there any official support for the same?

Or let me know what alternatives are available except the plugin (mongoose-view)
Kev
@lineus
May 15 2018 12:43
@irfaan008 mongoose doesn't have any built-in support for creating or destroying views, but if you have one created already you might just be able to declare a mongoose model('viewName', {}); and use that model for the supported read-only operations, find, findOne, etc.
Irfan Raza
@irfaan008
May 15 2018 12:44
Okay, thanks for guiding up. Let me proceed this way. Will update you shortly
Kev
@lineus
May 15 2018 12:47
it also might be worth a feature request or PR in mongoose, though I'm not sure how likely it would be to get approved as there doesn't appear to be any builtin methods on the native driver to do this. I haven't noticed any calls to db.runCommand in the mongoose source, but it's certainly a possibility.
Irfan Raza
@irfaan008
May 15 2018 12:51
I defined a new model like this
var mongoose = require('mongoose'); module.exports = mongoose.model('vwFeeds', {});
where vwFeeds is already available in my mongodb
but on running vwFeeds.find(), I don't get any record. Although there are more than 500 records in the real collection feeds
Anything which I am missing?
Kev
@lineus
May 15 2018 12:55
I'll have to create an example and play with it. It's certainly possible that it just won't work. I'll let you know what I find
Irfan Raza
@irfaan008
May 15 2018 12:55
Sure. I will wait.
Kev
@lineus
May 15 2018 13:22
@irfaan008 make sure mongoose isn't pluralizing your collection name
err, view name
you can use mongoose.set('debug', true) to verify that it's using the correct name. it worked for me.
though it looks like your view was already plural, so not sure :/
Irfan Raza
@irfaan008
May 15 2018 13:48
Okay.. let me create a singular view
Irfan Raza
@irfaan008
May 15 2018 14:07
Ahh.. setting debug helped me to notice that mongoose is converting the model name into lowercase. In my case vwFeed was getting converted into vwfeeds. And mongodb collection/view names are case sensitive so it didn't work. I now renamed my views to vw_feeds and in mongoose model I use name as vw_feed. At run time mongoose makes it plural automatically
Thanks @lineus . Your direction helped me to solve this.. Hope to see inbuilt support on mongoose for view..
Kev
@lineus
May 15 2018 14:41
@irfaan008 just FYI, you can use the createCollection method on mongoose.connection to create a view. gist here
Irfan Raza
@irfaan008
May 15 2018 14:43
Thanks @lineus ..
Kev
@lineus
May 15 2018 14:43
no worries :)
I didn't know we could do that already. I think it would be cool if there was an alias for createView or something, but at least it could have an example in the docs to make it easier to find. I'll put in a PR for it.
Irfan Raza
@irfaan008
May 15 2018 14:45
yes that would be better
One of the contributor answered this here actually - Automattic/mongoose#5694
Daniel Netzer
@DanielNetzer
May 15 2018 19:14
Hello everyone, weird question. when I save an object some properties in it are created with mongoose.schema.id in it, anyone know why something like this might happen?
Kev
@lineus
May 15 2018 19:20
@DanielNetzer subdocuments in mongoose get an _id property by default, this can be disabled with { _id: false } in the subdoc schema.
Daniel Netzer
@DanielNetzer
May 15 2018 19:24
but they aren
aren't subdocs, ill share the schema, 1 second
import mongoose from 'mongoose';

export interface CategoryAI {
    utterances: Utterance[];
    entities: string[];
}

export interface CategoryFilters {
    cases: string[];
    featuresEffected: [{
        name: string,
        operator: string,
        limitation: string
    }];
}

export interface BotMessage {
    type: BotMessageType;
    message: string;
    options?: Array<string>;
    results?: Array<ProductDetails>;
}

export interface ProductDetails {
    platformId: string;
    title: string;
    price?: string | number;
    imageUrl?: Array<string>;
    viewUrl: string;
    description: string;
}

export enum BotMessageType {
    Opening = 0,
    Text = 2,
    Result = 4,
    Action = 8
}

export type BotCategoryModel = mongoose.Document & {
    platformId: string;
    domain: string;
    name: string;
    imageUrl: string;
    description: string;
    viewUrl: string;
    ai: CategoryAI;
    products: Array<ProductDetails>;
    conversation: Array<BotMessage>;
    filters: CategoryFilters;
};

export interface Utterance {
    text: string;
    intentName: string;
    entityLabels: EntityLabel[];
}

export interface EntityLabel {
    entityName: string;
    startCharIndex: number;
    endCharIndex: number;
}

const botCategorySchema = new mongoose.Schema({
    platformId: String,
    name: String,
    imageUrl: String,
    description: String,
    domain: String,
    viewUrl: String,
    ai: {
        utterances: [{
            text: String,
            intentName: String,
            entityLabels: [{
                entityName: String,
                startCharIndex: Number,
                endCharIndex: Number
            }]
        }],
        entities: [String]
    },
    products: [{
        platformId: String,
        title: String,
        price: String || Number,
        imageUrl: [String],
        viewUrl: String,
        description: String
    }],
    conversation: [{
        type: { type: String, enum: ['Opening', 'Text', 'Result', 'Action'] },
        message: String,
        options: [String],
        results: [{
            title: String,
            price: String || Number,
            currency: String,
            imageUrl: [String],
            resultPageUrl: String
        }]
    }],
    filters: {
        cases: [String],
        featuresEffected: [{
            name: String,
            operator: String,
            limitation: String
        }]
    }
}, { timestamps: true });

botCategorySchema.set('toJSON', {
    virtuals: true,
    versionKey: false,
    transform: function (doc: mongoose.Document, ret: any) { delete ret._id; }
});

const BotCategory = mongoose.model('BotCategory', botCategorySchema);

export default BotCategory;
product is created with _id, Utterance is created with id
Kev
@lineus
May 15 2018 19:56
@DanielNetzer if you pull your subdoc objects into schemas of their own, you can remove the _id property with { _id: false } but the remove() embeddedDocument method will break. example
Daniel Netzer
@DanielNetzer
May 15 2018 20:06
ok, I now understand what you meant. so i'll keep the id's. thanks a lot @lineus
Kev
@lineus
May 15 2018 20:07
:smile: