我想检查我的 MongoDB 服务器是否正在运行。
我正在关注 thinkster.io 中关于MEAN 堆栈教程的教程。
我们制作持久数据存储的第一步是配置我们的数据模型。为此,我们将使用一个名为 Mongoose 的优秀库在 MongoDB 之上添加一个模式层。在我们开始之前,让我们确保我们的 MongoDB 服务器正在运行。
我尝试了很多东西,但我不知道该怎么做。我的主要问题是您是否必须启动服务器,然后打开另一个 cmd 并键入
mongod &或
停止服务器,然后键入
mongod &或在服务器运行时只需键入
mongod &相同的 cmd 而不停止服务器或启动另一个 cmd。
我有以下两个简单的查询:
Comment.aggregate([{$match: { _id: req.params.id }}])
.exec(function(err, result) {
// result is empty
});
Comment.find({ _id: req.params.id })
.exec(function (err, result) {
// correct result returned
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,聚合函数返回一个空数组。他们不应该返回相同的结果吗?
我正在研究一个保存到 Mongo的评论系统,它建议以下结构:
{
_id: ObjectId(...),
discussion_id: ObjectId(...),
slug: '34db',
posted: ISODateTime(...),
author: {
id: ObjectId(...),
name: 'Rick'
},
text: 'This is so bogus ... '
}
Run Code Online (Sandbox Code Playgroud)
我想保存author.id为ObjectId用户评论的,我在请求中有这个值req.user._id
我需要给我的 Comment 模型什么数据类型才能接受这个值?
我试过:
const authorSchema = new Schema({
id: ObjectId,
username: String
});
Run Code Online (Sandbox Code Playgroud)
但这给 ReferenceError: ObjectId is not defined
我看到ObjectId 列为有效的架构类型但是,它仅在自动生成时出现。
将user._idObjectId存储在注释中的正确方法是什么author.id,或者是否有更好的方法来完全存储引用?
将插件添加到我的架构后,我在控制器中使用 paginate 时遇到问题,我的代码是用 TypeScript 2.1 编写的,我在 devdependencies 中安装了 @types/mongoose-paginate。
[ts] 严重性:“错误”消息:“类型“模型”上不存在“属性”分页“。”
我的控制器:
export function getAllArtists(req, res) {
Artist.paginate({}, { page: 3, limit: 10 }, function(err, result) {
// ...
// ...
});
Run Code Online (Sandbox Code Playgroud)
我的架构:
'use strict'
import {Document, model, Model, Schema} from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate';
interface IArtist extends Document {
name: String;
description: String;
image: String;
}
const ArtistSchema: Schema = new Schema({
name: String,
description: String,
image: String
});
ArtistSchema.plugin(mongoosePaginate);
export const ArtistModel: Model<IArtist> = model<IArtist>('Artist', …Run Code Online (Sandbox Code Playgroud) var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Comment = mongoose.model('Comment', new Schema({
title : String,
body : String,
date : Date
}))
var Post = mongoose.model('Post', new Schema({
comments : [Comment]
}))
module.exports.Comment = Comment
module.exports.Post = Post
Run Code Online (Sandbox Code Playgroud)
遵循了一个简单应用程序的教程,在尝试从中创建其他内容并了解 Mongoose 模式时,我在尝试使用嵌入文档时出错
我收到这个错误
throw new TypeError('未定义类型
' + name + '在数组`' + path +
类型错误model:数组中的未定义类型comments
我有两个模式。
FAQ Schema
const EventFAQSchema = mongoose.Schema({
question: { type: String, req: true },
answer: { type: String, req: true }
});
EventSchema
const EventSchema = mongoose.Schema({
"title": { type: String },
"description": { type: String },
"eventFAQs": [{
type: EventFAQSchema ,
default: EventFAQSchema
}]
})
Run Code Online (Sandbox Code Playgroud)
我正在尝试嵌入一组 FAQ 类型的对象。但我收到以下错误
Undefined type 'undefined' at array 'eventFAQs'
我知道我缺少对象数组的基本概念。但是我花了很多时间试图找出原因并且无法自己解决。
我正在尝试将一个新元素推送到一个数组中,我在基于 express/nodejs 的 api 上使用了猫鼬。这是猫鼬的代码:
Serie.updateOne({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.episodes.videos.$.reports': data.details}},
function(err) {
if (err) {
res.status(500).send()
console.log(err)
}
else res.status(200).send()
})
Run Code Online (Sandbox Code Playgroud)
至于我的系列模型,它看起来像这样:
const serieSchema = new mongoose.Schema({
name: {type: String, unique:true, index: true, text: true},
customID: {type: Number, required: true, unique: true, index: true},
featured: {type: Boolean, default: false, index: true},
seasons: [{
number: Number,
episodes: [{number: Number, videos: [
{
_id: ObjectId,
provider: String,
reports: [{
title: {type: String, required: true},
description: String
}],
quality: {type: String, index: true, …Run Code Online (Sandbox Code Playgroud) 是否可以在猫鼬中进行递归?
例如,我想创建嵌套注释。
让我们考虑以下示例:
commentSchema = new mongoose.Schema({
comment : String,
author : String,
answers : [commentSchema] // ReferenceError: commentSchema is not defined
})
productSchema = new mongoose.Schema({
name : {type: String, required: true},
price : Number,
comments : [commentSchema]
})
Run Code Online (Sandbox Code Playgroud)
在 SQL 中,使用键很容易实现这一点。
首先我想到的是在commentSchema 中添加父字段,它将指向它正在回答的评论,但在这种情况下,如果评论只是数组中的一个简单对象,它们没有生成 id,所以这个解决方案在目前的设计中无法完成。
我想到的第二个解决方案是为评论创建一个单独的表,然后他们将拥有自己的 id,但这是在 mongodb 中进行的好方法吗?我的意思是它开始看起来与 SQL 表设计非常相似。
我有一个名为 UserSchema 的猫鼬模式,它存储有关所有用户的信息。我想让用户更改他的信息,我尝试使用 .findByIdAndUpdate。这是相关代码:
router.post("/updateprofile", function(req,res,next) {
const {id, org, tel, email, firstName, lastName} = req.body;
Users.findByIdAndUpdate(id, {org : org, tel : tel, email : email, firstName : firstName , lastName : lastName}, function (err, response) {
if (err) throw err
res.json(response);
});
});
Run Code Online (Sandbox Code Playgroud)
然而,试图改变信息的时候,我收到以下错误消息:Cannot read property 'password' of undefined。我很确定这是由预更新挂钩引起的,但我无法删除它,因为我的“忘记密码”功能需要它。这是代码:
UserSchema.pre('findOneAndUpdate', function (next) {
this.update({},{ $set: { password:
bcrypt.hashSync(this.getUpdate().$set.password, 10)}} )
next();
});
Run Code Online (Sandbox Code Playgroud)
我对它为什么使用那个 prehook 感到困惑,因为它在钩子中寻找findOneandUpdate以及当我尝试更改我正在使用的数据时findByIdAndUpdate。
我尝试使用,.update()但这也不起作用。有谁知道我做错了什么以及如何解决它?
您好,我想知道在保存文档的“状态”方面有什么更好的方法?我能想到的两种方法是使用字符串结尾枚举:
const proposal = new Schema({
state: {
type: String,
enum: ['pending', 'approved', 'denied'],
default: 'pending'
}
});
Run Code Online (Sandbox Code Playgroud)
或使用布尔值:
const proposal = new Schema({
approved: {
type: Boolean,
default: false
},
denied: {
type: Boolean,
default: false
}
});
Run Code Online (Sandbox Code Playgroud)
在性能和安全性方面,哪一个更好?在外面,搜索似乎booleans比string搜索更快。
mongoose-schema ×10
mongoose ×9
node.js ×7
mongodb ×4
aggregate ×1
arrays ×1
document ×1
express ×1
javascript ×1
pc ×1
state ×1
typescript ×1