Node.js 猫鼬不';不要创建新集合
我在server.js中有以下内容:Node.js 猫鼬不';不要创建新集合,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我在server.js中有以下内容: var mongoose = require('mongoose'), Schema = mongoose.Schema; 还有一个像这样的模型,很好用 var userSchema = new Schema({ firstName: { type: String, trim: true, required: true }, lastName: {type: String, trim: true, required:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
还有一个像这样的模型,很好用
var userSchema = new Schema({
firstName: { type: String, trim: true, required: true },
lastName: {type: String, trim: true, required: true},
cellPhoneNumber : {type: Number, unique: true},
email: { type: String, unique: true, lowercase: true, trim: true },
password: String
});
还有另一个模型,就像下面的模型一样,不起作用
var jobSchema = new Schema({
category: {type: Number, required: true},
title: {type: String, required: true},
tags: [String],
longDesc: String,
startedDate: Date,
views: Number,
report: Boolean,
reportCounter: Number,
status: String,
poster: String,
lastModifiedInDate: Date,
verified: Boolean
});
这两个变量如下所示:
var User = mongoose.model('User', userSchema);
var Job = mongoose.model('Job', jobSchema);
--连接server.js后,mongod不会记录任何错误。
有人知道我的第二个模型出了什么问题吗 在保存该模型的第一个文档之前,Mongoose不会为该模型创建
jobs
集合
Job.create({category: 1, title: 'Minion"}, function(err, doc) {
// At this point the jobs collection is created.
});
原因是,mongoose只会在启动时自动创建包含索引的集合。用户集合中有唯一索引,而作业集合中没有。我今天也有同样的问题
// example code to test
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
mongoose.model('Test', {
author: {
type: String,
index: true
}
});
首先要考虑的是,是否将连接字符串上的AutoDe指标属性设置为<强> true /false ; 默认情况下,autoIndex属性设置为True,mongoose将在连接时自动构建架构中定义的索引。这对于开发来说非常好,但对于大型生产部署来说并不理想,因为索引构建可能会导致性能下降。如果是这种情况,并且仍然没有在数据库中创建集合,那么问题可能是其他问题,与索引无关
如果将“自动索引”设置为false,mongoose将不会自动为与此连接关联的任何模型建立索引,即不会创建集合。在这种情况下,您必须手动调用model.ensureIndex();通常人们在定义模型的同一个地方或在控制器内部调用它,在我看来,这对生产是有害的,因为它做的事情与autoIndex相同,但这次我们明确地做了 我建议创建一个单独的node.js进程来显式运行EnsureIndex,并将其与主应用程序node.js进程分开 这种方法的第一个优点是我可以选择要运行EnsureIndex()的模型,第二个优点是它不会在应用程序启动时运行,会降低应用程序性能,而不是按需运行import mongoose from 'mongoose';
var readline = require('readline');
//importing models i want
import category from '../V1/category/model';
import company from '../V1/company/model';
import country from '../V1/country/model';
import item from '../V1/item/model';
//Connection string options
let options = {useMongoClient:true,
autoIndex:false, autoReconnect:true, promiseLibrary:global.Promise};
//connecting
let dbConnection = mongoose.createConnection('mongodb://localhost:1298/testDB', options);
//connection is open
dbConnection.once('open', function () {
dbConnection.modelNames()
.forEach(name => {
console.log(`model name ${name}`);
dbConnection.model(name).ensureIndexes((err)=> {
if(err) throw new Error(err);
});
dbConnection.model(name).on('index',function(err){
if (err) throw new Error(err);
});
});
console.log("****** Index Creation was Successful *******");
var rl = readline.createInterface({input:process.stdin,output:process.stdout});
rl.question("Press any key to close",function(answer){
process.exit(0);
});
});
下面是我用于按需运行EnsureIndex的代码示例
import mongoose from 'mongoose';
var readline = require('readline');
//importing models i want
import category from '../V1/category/model';
import company from '../V1/company/model';
import country from '../V1/country/model';
import item from '../V1/item/model';
//Connection string options
let options = {useMongoClient:true,
autoIndex:false, autoReconnect:true, promiseLibrary:global.Promise};
//connecting
let dbConnection = mongoose.createConnection('mongodb://localhost:1298/testDB', options);
//connection is open
dbConnection.once('open', function () {
dbConnection.modelNames()
.forEach(name => {
console.log(`model name ${name}`);
dbConnection.model(name).ensureIndexes((err)=> {
if(err) throw new Error(err);
});
dbConnection.model(name).on('index',function(err){
if (err) throw new Error(err);
});
});
console.log("****** Index Creation was Successful *******");
var rl = readline.createInterface({input:process.stdin,output:process.stdout});
rl.question("Press any key to close",function(answer){
process.exit(0);
});
});
这种行为的另一个解决方案是在一个模式对象中添加
unique:true
。这对我很有效,猫鼬自动创建了收藏
例如:
const moviesSchema = new Schema({
name: {
type: String,
required: true // I'm writting about such one
}
})
我认为不应该这样。Mongoose永远不会创建任何集合,除非您将保存/创建任何文档。感谢您的否决票,但您错了。自己测试一下。今天用猫鼬4.2.4重新测试。如果集合中有索引,则它会创建集合。由于这是基于场景的正确答案,因此它会工作。请检查下面的我的答案,只有当模型没有索引时,此答案才是正确的。添加“unique:true”确实创建了集合。但是,如果没有唯一键,则我认为将在save()unique:true上创建集合,它基本上会在该字段上添加索引,以便更快地进行验证查找,正如Derese Getachew所回答的那样。