我有mongodb中的Ebooks数据集合
{
"_id" : ObjectId("58b56fe19585b10cd42981d8"),
"cover_path" : "D:\\Ebooks\\uploads\\ebooks\\cover\\1488285665748-img1-700x400.jpg",
"path" : "D:\\Ebooks\\uploads\\ebooks\\pdf\\1488285665257-Webservices Natraz.pdf",
"description" : "ebook",
"title" : "book name",
"tag" : [
"Hindi",
"Other"
],
"__v" : NumberInt(0)
}
Run Code Online (Sandbox Code Playgroud)
现在我想搜索一些东西,如果关键字是一点点匹配,"title:"然后显示所有相关的书籍对象.
我的Mongoose架构是: -
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var EbookSchema = new Schema({
title: {type:String},
description: {type:String},
path: {type:String,required:true},
cover_path: {type:String,required:true},
tag: [{ type: String }]
});
module.exports = mongoose.model('Ebook', EbookSchema);
Run Code Online (Sandbox Code Playgroud)
我试试: -
app.get('/ebook?search=',function(req,res){
var search_key = req.param('search');
Ebook.find(title:'search',function(err, ebooks) {
if (err)
res.send(err);
res.json(ebooks);
}); …Run Code Online (Sandbox Code Playgroud) 我禁用互联网连接并运行节点服务器npm start,然后它抛出错误:

现在启用互联网连接并运行服务器工作正常:

所以,我想确认在启动服务器时是否真的需要连接到互联网?
如果它确实需要互联网连接,那么有任何想法让服务器脱机运行吗?
如果我连接到互联网并运行服务器,然后插上互联网连接,那么没有任何问题.一切正常.我不知道为什么只运行服务器需要互联网连接.
我有一个非常简单的mongo方案,我正在用猫鼬访问
我可以使用populate将用户名和名字映射到每个通知的字段,问题是我似乎无法在日期字段上进行任何排序
使用此代码我得到一个错误
MongooseError:无法在路径notification.from上填充sort,因为它是文档数组的子属性
是否有可能以不同的方式或更新的方式(深度填充,虚拟)这样做?我在使用Mongoose 5.
我宁愿不使用vanilla javascript来对对象进行排序或创建单独的模式
var UserSchema = new Schema({
username: String,
firstname: String,
notifications: [
{
from: { type: Schema.Types.ObjectId, ref: 'User'},
date: Date,
desc: String
}
]
});
app.get('/notifications', function(req, res) {
User.findOne({ _id: req._id }, 'notifications')
.populate({
path: 'notifications.from',
populate: {
path: 'from',
model: 'User',
options: { sort: { 'notifications.date': -1 } }
}
})
.exec(function(err, user) {
if (err) console.log(err)
})
});
Run Code Online (Sandbox Code Playgroud)
关于Mongo,这个可能重复的事实差不多已经有两年了.我问是否有更新或不同的方式在Mongoose中这样做,因为自2016年以来它已经改变了一些新的功能.
这里是我user和product架构:
const productSchema = new Schema({
//...
addedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "users"
}
});
const userSchema = new Schema({
//...
addedItems: [{
type: mongoose.Schema.ObjectId,
ref: "products"
}]
});
mongoose.model("products", productSchema);
mongoose.model("users", userSchema);
Run Code Online (Sandbox Code Playgroud)
在我的节点后端路线我做这个查询:
User.findOneAndUpdate(
{ _id: req.body.id },
{ $push: { addedItems: newProduct._id } },
{ upsert: true, new: true },
function(err, doc) {
console.log(err, doc);
}
);
Run Code Online (Sandbox Code Playgroud)
在console.log打印出这样的:
{
//...
addedItems: [ 5ab0223118599214f4dd7803 ]
}
Run Code Online (Sandbox Code Playgroud)
一切看起来都不错。我实际上是使用前端网站来查看我的mongo数据库的数据;我使用mlab.com,这是什么节目:
{
//...
"addedItems": [ …Run Code Online (Sandbox Code Playgroud) 我将基于mongodb设计一个群聊应用程序,有两种模式设计选择,一种设计为一个群组聊天消息的文档,另一种设计为所有群组消息的文档。
在第一个选项中,它可以显示为
var ChatMessageSchema = new Schema({
fromUserId: ObjectId,
toTroupeId: ObjectId,
text: String,
sent: Date
}
Run Code Online (Sandbox Code Playgroud)
在第二个选项中,它可以显示为
var ChatMessageSchema = new Schema({
toTroupeId: ObjectId,
chats:[
fromUserId: ObjectId,
text: String,
sent: Date
]
}
Run Code Online (Sandbox Code Playgroud)
两种设计都有优点和缺点,第二种选择的缺点是它几乎无法在用户上建立索引并搜索来自用户的消息,并且太多的组消息可能会迫使创建多个文档。
第一个选项似乎更合理,因为如果我们可以正确地建立索引,它可以允许基于groupid或userid搜索消息。
但我想知道,由于该组中有成千上万的消息,这意味着一个组中将有相应的成千上万的文档,这会影响数据库性能吗?
关于这些设计选择的任何想法,是第一个选择是最佳选择,还是如何对其进行优化?
我正在使用 useFieldArray 从后端 api 获取默认值。我的类别是一个字符串数组。但是,react-hook-form 仅支持对象数组。这是我的猫鼬的架构
type BookDocument = Document & {
title: string
description: string
categories: string[]
language: string
publicationYear: number
}
const bookSchema = new Schema(
{
title: { type: String, required: true },
description: { type: String, required: true },
categories: [{ type: String, requried: true }],
language: { type: String, required: true },
publicationYear: { type: Number, required: true },
},
{ timestamps: true }
)
Run Code Online (Sandbox Code Playgroud)
因此,我必须从前端修改我的表单,如下所示:
type FormData = {
title: string
description: …Run Code Online (Sandbox Code Playgroud) 我想开始利用Mongooses文档版本控制(__v key).我有一个实际增加版本值的问题,然后我发现你必须this.increment()在执行查询时添加.
有没有办法自动递增?目前,我刚刚将它添加到预中间件以获取更新类型的查询:
module.exports = Mongoose => {
const Schema = Mongoose.Schema
const modelSchema = new Schema( {
name: Schema.Types.String,
description: Schema.Types.String
} )
// Any middleware that needs to be fired off for any/all update-type queries
_.forEach( [ 'save', 'update', 'findOneAndUpdate' ], query => {
// Increment the Mongoose (__v)ersion for any updates
modelSchema.pre( query, function( next ) {
this.increment()
next()
} )
} )
}
Run Code Online (Sandbox Code Playgroud)
这似乎有用..但我有点认为在Mongoose中已经有办法做到这一点..我错了吗?
我在Mongoose> = 4.4中找不到任何涉及自定义对象(或值对象)的高级 自定义模式类型的示例.
想象一下,我想使用自定义类型,如:
function Polygon(c) {
this.bounds = [ /* some data */ ];
this.npoints = /* ... */
/* ... initialize polygon ... */
};
Polygon.prototype.area = function surfaceArea() { /**/ };
Polygon.prototype.toObject = function toObject() { return this.bounds; };
Run Code Online (Sandbox Code Playgroud)
接下来,我实现了一个自定义SchemaType,如:
function PolygonType(key, options) {
mongoose.SchemaType.call(this, key, options, 'PolygonType');
}
PolygonType.prototype = Object.create(mongoose.SchemaType.prototype);
PolygonType.prototype.cast = function(val) {
if (!val) return null;
if (val instanceof Polygon) return val;
return new Polygon(val)
}
PolygonType.prototype.default = …Run Code Online (Sandbox Code Playgroud) 使用以下架构:
{
data1: String,
nested: {
nestedProp1: String,
nestedSub: [String]
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,new MyModel({data1: 'something}).toObject()显示新创建的文档:
{
'_id' : 'xxxxx',
'data1': 'something',
'nested': {
'nestedSub': []
}
}
Run Code Online (Sandbox Code Playgroud)
即使用空数组创建嵌套文档.
如何使"嵌套"完全可选 - 即如果未在输入数据上提供,则根本不创建?
我不想为"嵌套" 使用单独的模式,不需要那种复杂性.
我正在尝试更新架构以添加新的属性字段.我希望它可以像将属性添加到模式一样简单,并且可以访问更新的字段.
我有一个现有的架构
let userDrinkSchema = new mongoose.Schema({ new Schema
creator : {
type: mongoose.Schema.Types.ObjectId,
ref: 'user' // name of user file
},
caffeine: Number,
mgFloz: Number,
name: String,
size: Number,
updated_at: {
type: Date,
default: Date.now()
}
});
Run Code Online (Sandbox Code Playgroud)
我需要从这个架构中添加id
const UserSchema = mongoose.Schema({
const User = module.exports = mongoose.model('User', UserSchema);
Run Code Online (Sandbox Code Playgroud)
我不知道如何为每个用户添加此属性.我按照这个例子 处理了Mongoose中的模式更改
架构现在是:
let DrinkSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
required: true,
default: null
},
caffeine: Number,
mgFloz: Number,
name: String,
size: Number,
imageUrl: String,
date: …Run Code Online (Sandbox Code Playgroud) mongoose-schema ×10
mongodb ×8
mongoose ×7
node.js ×7
express ×2
javascript ×2
database ×1
mean-stack ×1
reactjs ×1
typescript ×1