Tag: mongoose schema

mongoose加速在多个领域的search

我正在searchmongoose的文件,我必须search超过250,000个文件。 在这个function中,我必须在多个字段中添加search索引。 在文档中,一些字段是stringtypes,一些是多级对象。 我已经索引了所有可能的领域。 在当地我有10万份文件,当我search他们大概需要300-400毫秒。 但是,当我在服务器上search它们需要大约10-15秒的响应。 search查询是基于条件的,但我共享一个小的代码片段。 $and( { $or:[ {'field1': {$regex: re }}, {'field2': {$regex: re }}, {'level1.level2.value': {$regex: re }} ] }, { $and:[ { lowAge: {$lte: parseInt(age)} }, { highAge: {$gte: parseInt(age)} }, { $or:[ { gender:gender }, { gender:"N/A" } ] } ] } ) 有人可以告诉我,我怎么能加快服务器上的进程。

如何在应用程序之间共享Mongoose Schema

我有一个具有以下结构的应用程序: -MyApp -backend -app +mongoose-models +node_modules index.js package.json -admin +(lots of stuff) -frontend -app +app_controllers +node_modules +public +view index.js package.json “后端”文件夹是“pipe理员”的节点应用程序。 “admin”是应用程序pipe理的公共文件夹,“frontend”是访问者将看到的网站。 我想要做的是从“前端/应用程序/控制器”上的控制器访问“后端”文件夹内的mongoose模式/模型。 我尝试了这样的东西,但它并没有解决我的问题。 任何帮助将不胜感激。

NodeJS – .save()不是一个函数。 无法通过mongoose创build收集

我也面临.save()不是一个函数错误 ,但我已经经历了大多数类似的SO问题,并尝试各自的答案,但徒劳无功。 但通过我的terminal,我可以创buildcollections并保存一个。 另外.find()也在工作 。 以下是我的代码: 模型 – > clientUser.js var mongoose = require('mongoose'); var ClientUser = new mongoose.Schema({ firebaseUID :{type: String}, userName : { type : String}, displayName : { type : String} }); ClientUser.index({firebaseUID: 1}, {unique: true}); module.exports = { ClientUser : ClientUser }; dependency.js 'use strict'; var mongoose = require('mongoose'); var ClientUser […]

mongoosecollectionsdevise

这可能会从这里下降,但林困难包缠我的头这个问题。 在我的Mongoose Schema中,我有一个组列表。 这些小组将会是人,他们将会有一个_id,或者他们没有_id,他们会被他们的电子邮件列出。 对我来说,使用groups非常有意义的,这样我就可以循环访问列表,而不必硬编码列表名称,原因是我想让用户添加他们自己的组。 我不了解的是… 1)我怎样才能让用户添加到这个集合,我可以做groups.push(groupName) ? 不会把mongoose踢回去吗? 另外它是如何知道我想要在其他领域收集ID,名称,电子邮件。 2)我需要显示组的名称,然后分别显示组中的个人。 我不认为我可以用这种方式显示钥匙。 那么我怎样才能显示组的名称。 我认为我做这个根本错误,但我不能找出正确的方法 const mongoose = require("mongoose"), bcrypt = require('bcrypt-nodejs'); const passportLocalMongoose = require("passport-local-mongoose"); mongoose.createConnection("mongodb://localhost/familySite"); const userSchema = new mongoose.Schema({ username: {type:String, required: true, unique: true}, email: {type: String, required: false, unique: false}, password: { type: String, required: true}, resetPasswordToken: String, resetPasswordExpires: Date, profile: […]

查询以显示子文档不工作

我相当新mongoose,我想写一个查询,只是返回我的主文档中的一个子文档。 我的文档结构是这样的: PROFILE – CONTACTS [ARRAY] 我已经写了以下端点来返回一个联系人,我通过configuration文件ID和子文档的ID: “/:owerId / getcontact /:使用ContactID” 我的Expressfunction代码如下所示: router.route('/:ownerId/getcontact/:contactId') .get(function(req, res){ Profile.findOne({'owner_id':req.params.ownerId}).then(function(err, profile){ if(err) res.send(err); profile.findOne({'_id':req.params.contactId}).then(function(err, contact){ if(err) res.send(err); res.json(contact); }) }) }) 但是这个代码返回整个configuration文件。 当我试图通过身份证得到同样的事情。 这里是我的代码,下面是返回的整个configuration文件: router.route('/:ownerId/getcontact/:contactId') .get(function(req, res){ Profile.findById(req.params.ownerId).then(function(err, profile){ if(err) res.send(err); var data = profile.contacts.id(req.params.contactId); res.json(data); }) }) { "_id": "5886e3692ca4542c453431ee", "initial": "u", "last_name": "randal", "first_name": "chris", "owner_id": "5886e32c2ca4542c453431ed", […]

节点挂在mongoose的动作上

所以我正在用Mongoose和Node开发一个应用程序。 每当我尝试使用邮递员得到/api/users或后/api/users/name/name/email/test它只是挂起。 我已经确认它连接到数据库, app.listen()在db.once('open') 这是我的model.js 'use strict' var mongoose = require('mongoose'); var Schema = mongoose.Schema; const userSchema = new Schema({ first_name : String, last_name : String, email : { type : String, unique : true }, password : String, age : String, address : String, phoneNumber: String, gender : String, preffered : String, alerts : […]

Mongoose Schema扩展时间戳以具有新的属性(用户名)

我正在使用MongoDB和mongoose需要跟踪数据创build和更改的项目。 需求是我需要跟踪logging的创build和更改,包括date/时间和应用程序用户 (而不是操作系统用户)。 我已经看到了mongoose时间戳选项,可以解决我的date/时间问题,但有没有办法将其扩展到包括额外的属性,在那里我将添加应用程序的用户名? 如果没有,是否有一个地方我可以写一个函数,将在每次创build/更新时调用,以便我可以包含/修改这些字段? 今天,我在下面的每个模型中都join了这些属性,但我想把它们全部移到一个地方。 var companySchema = mongoose.Schema({ name: { type: String, index: true }, phone: { type: String }, deleted: { type: Boolean }, createdAt: { type: Date }, createdBy: { type: String }, updatedAt: { type: Date }, updatedBy: { type: String } }); 最好的办法是什么?

在MongoDB中存储长string会导致插入错误

我正在为我的大学构build一个类似于论坛的web应用程序,我正在实现的一个function是为用户编写针对特定考试试卷的答案的CRUDfunction。 但是,在testing过程中我发现,由于索引错误,我无法将string存储在答案文档的“详细信息”部分,如图所示。 {"success":false,"errors":["Exception: WriteError({\"code\":17280,\"index\":0,\"errmsg\":\"Btree::insert: key too large to index, failing maths_pyp.answers.$details_1 2265 { : \\\"…. 我在Express中使用了MongoDB,所以我select了mongoose作为我的MongoDB驱动。 “答案”文档的模式如下所示: 'use strict'; exports = module.exports = function(app, mongoose) { var answerSchema = new mongoose.Schema({ pivot: { type: String, default: '' }, module_code: {type: String, required: true}, _year_sem: {type: String, require: true}, author: { type: String, required:true}, author_id: {type: […]

在mongoose中组织静态函数

我使用mongodb在节点js中构build应用程序。 我正在使用mongoose作为ODM。 问题是我有很多静态函数在mongoose模型附加到模式。 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; var userSchema = new Schema({ profile: { firstName: { type: String }, lastName: { type: String } }, auth:{ username:{ type: String }, password:{ type: String } }, account:{ status:{ type: Boolean, default: false } } }); userSchema.statics.function1 = function(params, […]

Mongoose bcryptjs比较密码不参考文件(这个)

我有像这样的mongoose模式 var mongoose = require ('mongoose'); var bcrypt = require('bcryptjs'); var Schema = mongoose.Schema; var SALT_WORK_FACTOR = 10; var touristSchema = new Schema ({ local: { email: String, password: String }, facebook: { id: String, token: String, email: String, name: String, } }); touristSchema.pre('save', function(next) { var user = this; console.log('bcrypt called by strategy', user); […]