Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle nested field? The Anagram is not created under info.name1 #78

Closed
egonzal93 opened this issue Nov 16, 2020 · 7 comments
Closed

Comments

@egonzal93
Copy link

egonzal93 commented Nov 16, 2020

My model looks like this:

const taskSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  info: {
    name1: String,
    title: String,
    version: String,
    description: String
  },
  completed: {
    type: Boolean,
    required: true
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
}, {
    timestamps: true
});

taskSchema.plugin( mongoose_fuzzy_searching, {
    fields: [{
        name: 'info.name1',
        minSize: 2,
        prefixOnly: false,
    }]
    }
);

I saved :

 const debug = require('debug')('add_new_task');
    const input = {
        name: taskTitle,
        info: {
            name1: "Army Ants1",
            title: "Army Ants2",
            version: "Army Ants3",
            description: "Army Ants4"
        },
        completed: false
    };
    try {
        const user = await User.findOne({email: taskEmail});
        debug(`user ${JSON.stringify(user, null,2)}`);
        const task = new Task({...input, user: user.id});
        const result = await task.save();
        user.tasks.push(result.id);
        await user.save();
        debug(`result=${JSON.stringify(result, null,2)}`);
        return result;
    } catch (error) {
        console.log(error);
    }

The Anagram is not created under info.name1:

add_new_task result={
add_new_task "info": {
add_new_task "name1_fuzzy": [
add_new_task ""
add_new_task ],
add_new_task "name1": "Army Ants1",
add_new_task "title": "Army Ants2",
add_new_task "version": "Army Ants3",
add_new_task "description": "Army Ants4"
add_new_task },
add_new_task "_id": "5fb3130d6d087b31b974acff",
add_new_task "name": "Army of Ants",
add_new_task "completed": false,
add_new_task "user": "5f91caa051fa0512993e9326",
add_new_task "createdAt": "2020-11-17T00:02:22.006Z",
add_new_task "updatedAt": "2020-11-17T00:02:22.006Z",
add_new_task "__v": 0
add_new_task } +167ms

name1_fuzzy is empty.

@egonzal93 egonzal93 changed the title How to handle nested field? How to handle nested field? The Anagram is not created under info.name1 Nov 17, 2020
@VassilisPallas
Copy link
Owner

VassilisPallas commented Nov 17, 2020

According to the documentation you need to use keys for nested attributes.
https://github.com/VassilisPallas/mongoose-fuzzy-searching#object-field

@egonzal93
Copy link
Author

I used keys:

const mongoose = require('mongoose');
const mongoose_fuzzy_searching = require('mongoose-fuzzy-searching');

const taskSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
info: {
name1: String,
title: String,
version: String,
description: String
},
completed: {
type: Boolean,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}, {
timestamps: true
});

taskSchema.plugin( mongoose_fuzzy_searching, {
fields: [{
name: 'info',
keys: [
'name1',
'title',
'version',
'description'
]
}]
}
);

module.exports = mongoose.model('Task', taskSchema);

Saved it:

addNewTask = async () => {
const debug = require('debug')('add_new_task');
const input = {
name: taskTitle,
info: {
name1: "Army1 Ants1",
title: "Army1 Ants2",
version: "Army1 Ants3",
description: "Army1 Ants4"
},
completed: false
};
try {
const user = await User.findOne({email: taskEmail});
debug(user ${JSON.stringify(user, null,2)});
const task = new Task({...input, user: user.id});
const result = await task.save();
user.tasks.push(result.id);
await user.save();
debug(result=${JSON.stringify(result, null,2)});
return result;
} catch (error) {
console.log(error);
}

but still anagrams are not getting saved to mongo:

Mongoose: tasks.insertOne({ _id: ObjectId("5fb326440deaa5357845fa8b"), name: 'Army1 of Ants', info: { name1: 'Army1 Ants1', title: 'Army1 Ants2', version: 'Army1 Ants3', description: 'Army1 Ants4' }, completed: false, user: ObjectId("5f91caa051fa0512993e9326"), info_fuzzy: [ { name1_fuzzy: [ 'y1', 'my', 'rm', 'ar', 'my1', 'rmy', 'arm', 'rmy1', 'army', 'army1', 's1', 'ts', 'nt', 'an', 'ts1', 'nts', 'ant', 'nts1', 'ants', 'ants1', 'army1 ants1' ], title_fuzzy: [ 'y1', 'my', 'rm', 'ar', 'my1', 'rmy', 'arm', 'rmy1', 'army', 'army1', 's2', 'ts', 'nt', 'an', 'ts2', 'nts', 'ant', 'nts2', 'ants', 'ants2', 'army1 ants2' ], version_fuzzy: [ 'y1', 'my', 'rm', 'ar', 'my1', 'rmy', 'arm', 'rmy1', 'army', 'army1', 's3', 'ts', 'nt', 'an', 'ts3', 'nts', 'ant', 'nts3', 'ants', 'ants3', 'army1 ants3' ], description_fuzzy: [ 'y1', 'my', 'rm', 'ar', 'my1', 'rmy', 'arm', 'rmy1', 'army', 'army1', 's4', 'ts', 'nt', 'an', 'ts4', 'nts', 'ant', 'nts4', 'ants', 'ants4', 'army1 ants4' ] } ], createdAt: new Date("Tue, 17 Nov 2020 01:24:20 GMT"), updatedAt: new Date("Tue, 17 Nov 2020 01:24:20 GMT"), __v: 0 }, { session: null })
Mongoose: users.updateOne({ _id: ObjectId("5f91caa051fa0512993e9326") }, { '$push': { tasks: { '$each': [ ObjectId("5fb326440deaa5357845fa8b") ] } }, '$set': { updatedAt: new Date("Tue, 17 Nov 2020 01:24:20 GMT") }, '$inc': { __v: 1 } }, { session: undefined })
add_new_task result={
add_new_task "_id": "5fb326440deaa5357845fa8b",
add_new_task "name": "Army1 of Ants",
add_new_task "info": {
add_new_task "name1": "Army1 Ants1",
add_new_task "title": "Army1 Ants2",
add_new_task "version": "Army1 Ants3",
add_new_task "description": "Army1 Ants4"
add_new_task },
add_new_task "completed": false,
add_new_task "user": "5f91caa051fa0512993e9326",
add_new_task "createdAt": "2020-11-17T01:24:20.268Z",
add_new_task "updatedAt": "2020-11-17T01:24:20.268Z",
add_new_task "__v": 0

@egonzal93
Copy link
Author

NVM, it worked this time: Thanks for the response. Good night.

{"_id":{"$oid":"5fb3286e8b01a735eb82f5dc"},"name":"Army1 of Ants","info":{"name1":"Army1 Ants1","title":"Army1 Ants2","version":"Army1 Ants3","description":"Army1 Ants4"},"completed":false,"user":{"$oid":"5f91caa051fa0512993e9326"},"info_fuzzy":[{"name1_fuzzy":["y1","my","rm","ar","my1","rmy","arm","rmy1","army","army1","s1","ts","nt","an","ts1","nts","ant","nts1","ants","ants1","army1 ants1"],"title_fuzzy":["y1","my","rm","ar","my1","rmy","arm","rmy1","army","army1","s2","ts","nt","an","ts2","nts","ant","nts2","ants","ants2","army1 ants2"],"version_fuzzy":["y1","my","rm","ar","my1","rmy","arm","rmy1","army","army1","s3","ts","nt","an","ts3","nts","ant","nts3","ants","ants3","army1 ants3"],"description_fuzzy":["y1","my","rm","ar","my1","rmy","arm","rmy1","army","army1","s4","ts","nt","an","ts4","nts","ant","nts4","ants","ants4","army1 ants4"]}],"createdAt":{"$date":"2020-11-17T01:33:34.923Z"},"updatedAt":{"$date":"2020-11-17T01:33:34.923Z"},"__v":0}

@egonzal93
Copy link
Author

It looks like the old key indexes needs to be dropped when you change keys.

@VassilisPallas
Copy link
Owner

Yes indeed. I'm currently working on deleting the indexes every time.

@egonzal93
Copy link
Author

egonzal93 commented Nov 17, 2020

Do you know when you plan on releasing an update? Should the index expire so that it can be rebuilt?

@VassilisPallas
Copy link
Owner

I think we can expire only date indexes. The ideal scenario is every time you restart your application to delete manually the indexes. I will try to delete them when the plugin is initialized.
However, I don't know if this is possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants