Tag: mongoose

Mongoose在嵌套的对象数组中推送多个唯一值

我试图在嵌套的对象数组中推入多个值,如下所示: Schema.update( { _id: req.params.id}, { $push: { content: { $each: req.body } } } 作为req.body这样的东西: req.body = [{id: 1, name: 'foo'},{id: 2, name: 'bar'}] 确保它们中没有一个已经存在,有任何方法可以将$ ne属性用于多个子元素? 提前致谢。

reddit评论系统如何在mongodb / nodejs中复制

如何在MongoDB / mongoose中创build嵌套的评论回复,以便像在reddit嵌套中一样创build评论系统。 这是我迄今创build的模式: var CommentSchema = Schema({ body: {type: String}, chapterId: {type: RK, ref: 'Chapter'}, by: {type: RK, ref: 'User'}, }, {timestamps: true}); var UserSchema = new Schema({ name: String, username: {type:String, unique: true}, profile_pic: String, password: String, role:{type: [{ type: String, enum: ['user', 'admin'] }], default: ['user'] } });

如何增加Mongoose中子文档的属性?

我正在使用Mongoose在NodeJS中增加一个对象的子文档的评级。 我将在下面发布模型和路线代码。 当我在Postman中执行查询时,我得到一个空的json对象,并返回一个400错误。 这意味着在查询的某个地方我做错了什么。 blogPost模型 const BlogPostSchema = new Schema({ content: { type: String, validate: { validator: (content) => content.length > 5, message: 'Content must contain at least 6 characters.' }, required: [true, 'Content must be filled in.'] }, rating: Number, title: String, user: { type: Schema.Types.ObjectId, ref: 'user' }, board: {type: Schema.Types.ObjectId, ref: 'board'}, […]

Mongoose用.find()返回空数组

我试图从我的应用程序使用Mongoose模块查询我的MongoDB数据库中的数据,返回的是一个空的数组。 SO中最常见的答案是在创build模型时,Mongoose将集合名称复数化。 不过,我相信我在那里没有问题(如果实际上我做错了,请给我解释一下)。 在这一点上,我尝试过的所有操作都是使用Result.find({},callback)select所有条目,但是同样返回一个空数组。 当我查询命令行上的数据时,我得到了预期的结果。 下面我简单地检索了一个条目。 命令行上的Mongo shell: > db.getName() kaplanFfl > db.getCollection('results') kaplanFfl.results > db.results.findOne({ Owner: 'Sample'}) { "_id" : ObjectId("5a24c90b46521500830aa042"), "Owner" : "Sample", "Pts" : 81.5, "Week" : 1, "SeasonAvg" : 86, "Lst8_Avg" : 92, "Lst4_Avg" : 91, "Mean" : 82, "High" : 116, "Low" : 55, "Diff" : 61, "Dev" : 16.49141822, […]

带前钩的mongoose挽救计划不起作用

我是Nodejs和Mongoose的新手,事情一切正常,直到新版发布的承诺折旧的mongoose。 我看到它build议使用蓝鸟,我正在尝试做。 我有以下代码: app.post('/users', (req, res) => { var body = _.pick(req.body, ['email', 'password']); var user = new User(body); user.save().then(() => { return user.generateAuthToken(); }).then((token) => { res.header('x-auth', token).send(user); }).catch((e) => { res.status(400).send(e); }) }); 和前钩: UserSchema.pre('save', function (next) { var user = this; if (user.isModified('password')) { bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(user.password, salt, (err, […]

mongoosesorting和search文件由填充字段

我收集用户,新闻和用户新闻,“用户新闻”填充到“用户”和“新闻”。 我可以通过“user.name”或“news.title”来sorting和search“user-news”的文档吗? const usersSchema = new Schema({ name: String }) const newsSchema = new Schema({ title: String }), UsersNewsModel.find(queryObj) .populate('user', 'name') .populate('news', 'title') .sort({ 'user.name': -1 }) .exec((findError, records) => {…}

mongoose设置默认值并插入数据库

我对Mongo / Mongoose非常陌生。 情况是这样的:设置和更新默认值find所有条目,但我想我错过了一些东西 它正在Web视图上工作,但是在find多个条目后,它不会显示在数据库中。 我必须单击Web视图上的每个条目以将其保存到数据库 var gcalbookingSchema = new Schema({ gcalId: String, training: {type: String, default: "work out" }, summary: String, email: String, mobile: {type: String, default: "12345678" }, datetime: Date, notification: {type: String, default: "24 hours" }, attendees: [[]] }); 这是我的代码来查找多个条目并将其显示在Web视图上。 我尝试使用选项与setDefaultsOnInsert:true,但它扔了callback.apply不是一个函数 app.get("/api/gcal", function (req, res) { var gemail = req.query.email; var options […]

迭代对象作为通过邮递员工具发送的对象数组,并保存在数据库中

这是我通过邮递员工具发送的价值 { "name" :[ { "first_name" : "antony", "second_name" : "grijan" },{ "first_name" : "suresh", "second_name" : "muthu" }], "allergy" : [ { "condition" : "headache" }, { "condition" : "toothache" }], "communication" : [ { "address" : "no 21 big street", "phone" : "84" }, { "address" : "no 43 small street", "phone" :"87" […]

区分节点的本地function和匿名function

我正在寻找一种方法来遍历一个对象,并执行一个动作,如果我有一个匿名函数,并不执行操作,如果我有一个本地的function。 最好用一个例子来解释: User = { first : String, last : String, email : function(email) { // …. } } User.forEach(function(prop) { if(! native ) { // Do something } }); 消化道反应当然不是,但是console.log会输出string原生函数的[Function: String] [Function] ,匿名函数的[Function] 。 谢谢! 马特

我如何使用Mongoose进行查询,然后拿这个文档做其他事情?

User.findOne({}, function(err, doc){ //do stuff with "doc" here. }); 这就是我现在这样做的方式。 但是,如果我想采取“文件”,只是做的东西。 User.findOne({}, function(err, doc){ //do stuff with "doc" here. }); runAnotherFunction(doc.name) doc… 如何从“function”部分取出“doc”?