Comments (7)
According to the documentation you need to use keys
for nested attributes.
https://github.com/VassilisPallas/mongoose-fuzzy-searching#object-field
from mongoose-fuzzy-searching.
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
from mongoose-fuzzy-searching.
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}
from mongoose-fuzzy-searching.
It looks like the old key indexes needs to be dropped when you change keys.
from mongoose-fuzzy-searching.
Yes indeed. I'm currently working on deleting the indexes every time.
from mongoose-fuzzy-searching.
Do you know when you plan on releasing an update? Should the index expire so that it can be rebuilt?
from mongoose-fuzzy-searching.
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
from mongoose-fuzzy-searching.
Related Issues (20)
- Fuzzy Search not working if query is provided HOT 1
- Getting MongoServerError: language override unsupported when plugin is used in a collection with the field language with invalid values
- Update mongoose version to 6.x.x HOT 2
- Working with unique values
- [Feature Request] Add pagination options! HOT 2
- Using keys with dynamic object values, does not set fuzzy values HOT 1
- does it support arabic? HOT 1
- TypeError: obj.fromObjectKeys is not a function HOT 3
- Cannot find some string values HOT 2
- Updating Fields to Empty String
- Add option to use $all filter instead of $text search HOT 2
- How can I implement the partial index on this package ?
- Results accuracy
- Exact match result is not the first item returned HOT 2
- Search in Referenced Object Id Field HOT 2
- How to use with Aggregation? HOT 1
- how to show some related data if no data found?
- update to mongoose 6.x.x HOT 4
- Search not working with object data
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-fuzzy-searching.