如果字段不为空,是否有内置的方法来说明字段应该是唯一的。例如,我希望用户拥有唯一的电话号码,但这不是必填字段。因此它可以为空,因此如果添加了另一个没有电话号码的用户,它将打破唯一约束。
这几乎是同一个问题:mongoDB/mongoose: unique if not null但对于mongoose和 mongodb 的较新版本,我想应该有更好的方法来实现这一点。
我以前有一个这样的架构
var schema = mongoose.Schema({
type: String,
name: String,
});
Run Code Online (Sandbox Code Playgroud)
现在我的架构发生了变化
var schema = mongoose.Schema({
type: String,
name: String,
content: {
title: {type: String, default: 'Some Title'},
description: {type: String, default: 'Some Description'}
}
});
Run Code Online (Sandbox Code Playgroud)
现在 Schema 已更改,我想使用 dafault 值更新集合中以前的文档,该怎么做?
第二周,我尝试在apollo-server-express / MongoDB / Mongoose / GraphQL堆栈中链接两个集合,但我不知道如何。我在REST API中找到了类似的课程,我需要的是“关系”。我需要这个,但是在GraphQL中
如何为用户添加汽车?我收集了测试服务器,代码在这里:https : //github.com/gHashTag/test-graphql-server 帮助
我正在尝试发送一个总付费和未付费客户端列表以及来自我的节点 API 的数据。在猫鼬方法中,我一直在思考如何走得更远。
任何人都可以建议实现这一目标的最佳方法吗?
router.get("/", ensureAuthenticated, (req, res) => {
Loan.aggregate([
{
$match: {
ePaidunpaid: "Unpaid"
}
}
]).then(function(data) {
console.log(data);
res.render("dashboard", { admin: req.user.eUserType, user: req.user,data:data });
});
});
Run Code Online (Sandbox Code Playgroud)
贷款模式:
const Loan = new Schema({
sName: { type: String },
sPurpose: [String],
sBankName: String,
sBranchName: [String],
nTotalFees: { type: Number },
ePaidunpaid: { type: String ,default:'Unpaid'},
sCashOrCheque: { type: String },
});
Run Code Online (Sandbox Code Playgroud)
结果: 具有付费和未付费客户计数的用户详细信息
[
Paid:{
// Paid users
},
Unpaid:{
// Unpaid Users
}, …Run Code Online (Sandbox Code Playgroud) 我有 Encomenda,它有一个 Itens 数组。Itens 可以有一个 itens 数组。我有以下代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var idvalidator = require('mongoose-id-validator');
let ItemSchema = new Schema({
produtoId:Number,
itens:[{type:Schema.Types.ObjectId, ref: 'Item'}]
});
function autoPopulateItens(next){
this.populate('itens');
next();
}
ItemSchema
.pre('findOne',autoPopulateItens)
.pre('find',autoPopulateItens);
module.exports=mongoose.model('Item',ItemSchema);
let EncomendaSchema= new Schema({
itens:[ItemSchema]
});
module.exports=mongoose.model('Encomenda',EncomendaSchema);
function log(data){
console.log(JSON.stringify(data,undefined,2))
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试向邮递员发出帖子请求,以使用以下代码创建一个 Encomenda:
{
"itens":[{
"produtoId":5,
"itens":[{
"produtoId":6,
"itens":[]
},{
"produtoId":7,
"itens":[]
},{
"produtoId":8,
"itens":[]
}]
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试创建一个 Encomenda 并将其保存到 mongodb 时,它给了我一个错误:
CoreMongooseArray [ { itens: [], _id: 5bd9fde20c29e35f2c0ca74a, produtoId: …Run Code Online (Sandbox Code Playgroud) 我想从collection1一直到Collection 3和4进行查找,以在一个查询中比较名称。
例:
collection1.find({
collection2.collection3.name: req.body.name3,
collection2.collection4.name: req.body.name4
}).exec()
Run Code Online (Sandbox Code Playgroud) 我假设在字段中添加unique:true将停止使用相同的值保存到数据库。但是即时消息仍然允许这样做。
“猫鼬”:“ ^ 5.4.19”,
const SessionSchema = new Schema({
jobId: {
type: String,
required: false,
unique: true,
index: true,
},
productId: {
type: String,
required: true,
},
status: {
type: String,
default: "Pending",
},
mode: {
type: String,
default: "authentication",
},
result: {
type: Schema.Types.Mixed,
},
requests: [RequestSchema],
callback_at: {
type: Date,
},
}, {
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除并重新创建集合。参见下图,我可以使用相同的jobId创建1的新会话。
public store = async (req: Request, res: Response): Promise<any> => {
const input = req.body;
let …Run Code Online (Sandbox Code Playgroud) db.bios.find()
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我有这部分:
mongoose.connect('mongodb://localhost:27017/eBookStore');
let newBookSchema = new mongoose.Schema({
bookName: {type: String},
bookSubtitle: {type: String},
publicationDate: {type: Number, default: new Date().getTime()}
});
let Book = mongoose.model('books', newBookSchema);
db.Book.find();
Run Code Online (Sandbox Code Playgroud)
其中“eBookStore”是我的数据库名称,“books”是我的收藏名称。我知道我在 'db.Book.find()' 中输入 'db' 的地方不正确,但我不知道在引用数据库时那里的代码应该是什么样子。请帮忙!
我正在尝试使用 .inertMany但我无法插入数据为什么?我正在使用mongoose session如果发生任何错误然后我回滚更改
https://codesandbox.io/s/dreamy-bell-9u0bz
app.get("/saveData", async (req, res, next) => {
const session = await mongoose.startSession();
session.startTransaction();
try {
const data = [
{
empid: "Ad",
id: 4,
date: "19-Jul-2019"
},
{
empid: "Bc",
id: 56,
date: "18-Jul-2019"
},
{
empid: "C",
id: 6,
date: "11-Jul-2019"
}
];
console.log("before save");
let saveBlog = await BlogPostModel.insertMany(data, { session }); //when fail its goes to catch
await session.commitTransaction();
return res.send(saveBlog);
} catch (error) {
console.log(error);
await session.abortTransaction();
return …Run Code Online (Sandbox Code Playgroud) 将多个对象推入数组时遇到很多麻烦。
我尝试了此代码的几种不同变体,最终要么将对象直接推送到数据库,要么只推送数组的最后一个对象
这是我目前正在尝试的代码,它有效,但只推送最后一个对象(在本例中为星期三)。
collection.findOneAndUpdate(
{ name: 'yyy' },
{ $push: { schedule: monday, schedule: tuesday, schedule: wednesday}},
function (error, success) {
if (error) {
console.log("error");
console.log(error);
} else {
console.log("success");
console.log(success);
}
});
Run Code Online (Sandbox Code Playgroud)
我试过了
collection.findOneAndUpdate(
{ name: 'yyy' },
{ $push: { schedule: monday, tuesday, wednesday}}
Run Code Online (Sandbox Code Playgroud)
它只是将星期二和星期三推到主要位置,而不是将它们放入日程表数组中。
这是我用于计划的架构
schedule: [
{
day: { type: String, default: ""},
closed: { type: Boolean, default: false },
start: { type: Number, default: 0},
startap: { type: String, default: "AM"},
end: { type: Number, …Run Code Online (Sandbox Code Playgroud)