当我使用mongoose时,我发现了两种在nodejs中创建新文档的方法.
第一:
var instance = new MyModel();
instance.key = 'hello';
instance.save(function (err) {
//
});
Run Code Online (Sandbox Code Playgroud)
第二
MyModel.create({key: 'hello'}, function (err) {
//
});
Run Code Online (Sandbox Code Playgroud)
有什么区别吗?
我想使用相同的正则表达式查询多个字段.在此查询中,我想接受单个文本输入,并检查firstName和lastName字段的结果.我可以使用mongoose文档中的regex函数来查询单个字段,但是'or'子句的语法给我带来了麻烦.
var re = new RegExp(req.params.search, 'i');
app.User.find().or([{ 'firstName': { $regex: re }}, { 'lastName': { $regex: re }}]).sort('title', 1).exec(function(err, users) {
res.json(JSON.stringify(users));
});
Run Code Online (Sandbox Code Playgroud)
(我在node.js 0.6.12上运行mongoose 2.7.1)
有没有办法删除Mongoose中父级的所有子级,类似于使用MySQL的外键?
例如,在MySQL中,我将分配一个外键并将其设置为在删除时级联.因此,如果我要删除客户端,则也会删除所有应用程序和关联用户.
从顶层:
抽奖和提交都有一个client_id字段.提交的字段包含sweepstakes_id和client_id.
现在,我正在使用以下代码,我觉得必须有更好的方法.
Client.findById(req.params.client_id, function(err, client) {
if (err)
return next(new restify.InternalError(err));
else if (!client)
return next(new restify.ResourceNotFoundError('The resource you requested could not be found.'));
// find and remove all associated sweepstakes
Sweepstakes.find({client_id: client._id}).remove();
// find and remove all submissions
Submission.find({client_id: client._id}).remove();
client.remove();
res.send({id: req.params.client_id});
});
Run Code Online (Sandbox Code Playgroud) 我是比较新的Node.js和受蒙戈/猫鼬,和我有一个非常困难的时期排查特定错误猫鼬:
VersionError:找不到匹配的文档.
(此问题底部的整个错误跟踪/堆栈.)
这篇博文非常清楚地概述了如何发生VersionError:
(TL; DR - "Mongoose v3现在为每个文档添加了一个模式可配置的版本密钥.只要对数组的修改可能会更改任何数组的元素位置,该值就会以原子方式递增."如果您尝试保存文档,但版本key不再匹配您检索到的对象,您可以获得上述内容VersionError.)
核心问题:有没有办法显示违规save()操作?或哪个文件未能保存?还是什么都没有?!;)
挑战:这是一个包含许多阵列的相对较大的代码库,我不确定如何开始解决问题.特别是,错误跟踪/堆栈似乎没有显示问题存在的位置.见下文:
VersionError: No matching document found.
at handleSave (<project_path>/node_modules/mongoose/lib/model.js:121:23)
at exports.tick (<project_path>/node_modules/mongoose/lib/utils.js:408:16)
at null.<anonymous> (<project_path>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection.js:484:9)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (<project_path>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:391:25)
at Server.connect.connectionPool.on.server._serverState (<project_path>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:558:20)
at MongoReply.parseBody (<project_path>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:131:5)
at Server.connect.connectionPool.on.server._serverState (<project_path>/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:22)
at EventEmitter.emit (events.js:96:17)
Run Code Online (Sandbox Code Playgroud) 我有以下mongoose模式:
var SimSchema = new Schema({
msisdn : { type : String , unique : true, required : true },
imsi : { type : String , unique : true, required : true },
status : { type : Boolean, default: true},
signal : { type : Number },
probe_name : { type: String , required : true }
});
Run Code Online (Sandbox Code Playgroud)
我有unique选择msisdn和imsi.
在某些情况下,这种情况得到了很好的尊重.对于以下mocha测试:
"use strict";
var app = require('../../app');
var http = require('http'); …Run Code Online (Sandbox Code Playgroud) 我有以下架构(道歉,它是在coffeescript)
Schema = mongoose.Schema
AuthS = new Schema
auth: {type: String, unique: true}
nick: String
time: Date
Auth = mongoose.model 'Auth', AuthS
Run Code Online (Sandbox Code Playgroud)
我只想恢复一条肯定在我的数据库中的记录:
Auth.findOne({nick: 'noname'}, function(obj) { console.log(obj); });
Run Code Online (Sandbox Code Playgroud)
很不幸,这始终记录null.db.auths.findOne({nick: 'noname'})在mongo shell中总是返回一个值.到底是怎么回事?
只是一个简单的查询,例如在模型中使用双引用.
架构/模型
var OrderSchema = new Schema({
user: {
type : Schema.Types.ObjectId,
ref : 'User',
required: true
},
meal: {
type : Schema.Types.ObjectId,
ref : 'Meal',
required: true
},
});
var OrderModel = db.model('Order', OrderSchema);
Run Code Online (Sandbox Code Playgroud)
询问
OrderModel.find()
.populate('user') // works
.populate('meal') // dont works
.exec(function (err, results) {
// callback
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试过类似的东西
.populate('user meal')
.populate(['user', 'meal'])
Run Code Online (Sandbox Code Playgroud)
实际上只有一个填充物可以工作.
那么,如何让两个填充工作?
这是问题的简化版本,但基本上我试图用mongoose打开2个mongodb连接,它给了我"试图打开未关闭的连接".错误.
代码示例:
var db1 = require('mongoose');
db1.connect('my.db.ip.address', 'my-db');
var db2 = require('mongoose');
db2.connect('my.db.ip.address', 'my-db');
db2.connection.close();
db1.connection.close();
Run Code Online (Sandbox Code Playgroud)
知道如何让它工作吗?
在查看教程时,通常会在模式和模型之间进行描述,尤其是在处理mongoose/mongodb时.这使得移植到postgresql有点令人困惑,因为在该系统下似乎不存在"模型".这两种方法有什么区别?
例如,这行的postgres/sql ORM是什么?
(mongoose和express.js):
var userSchema = schema.define('local', {
username: String,
password: String,
});
module.exports = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud) 我正在为一个新项目设计数据库结构,我对MongoDB很新,显然是Mongoose.
我已经阅读过Mongooses 人口文档,它有一对多的关系,一个Person文档到很多Story文档,但是令我困惑的部分是在哪里而不是Story引用Person它所属的文档的文档,Person架构设置了它所以它有一个Story"拥有"的文件数组.
我正在设置与此类似的东西.但我一直认为创建新Story文档以获取Person文档ID 会更容易.但也许这只是因为我更熟悉使用连接的MySQL关系.
如果这是最好的方法(并且我确定它是,因为它在文档中),在Story创建新文档时,最新的方法是更新People它所属的相关文档中的故事数组?我看了但找不到任何更新现有文档的例子来添加对其他文件的引用(或删除它们)
我确信这是一个简单的解决方案,我只是忽略了什么,但任何帮助都会很棒.谢谢!
mongoose ×10
mongodb ×9
node.js ×8
coffeescript ×1
database ×1
express ×1
javascript ×1
mocha.js ×1
postgresql ×1
regex ×1