Tag: mongoose

Express正在发送一个对象,里面有一个对象

我有一个连接到Mongoose数据库的Express服务器(我的术语可能不正确,因为我刚开始学习Express)。 当我访问一个/pipe理员(路线?)我收到一个对象与我的JSON对象里面。 我发错了吗? var userSchema = new Schema({ firstName: {type: String, trim: true}, lastName: {type: String, trim: true}, classYear: Number, email: {type: String, unique: true, sparse: true, trim: true}, phone: {type: String, unique: true, sparse: true}, phoneProvider: {type: String, trim: true}, isAdmin: {type: Boolean, index: true}, isSuperAdmin: {type: Boolean, index: true}, hash: String, companyName: {type: […]

函数中的Mongoose查询承诺让代码变得丑陋

我正在创build我的用户API,我想检查用户名是否被使用。 所以我写了一个静态函数 static findByName(name) { const query = User.where({ username: name, }); query.findOne((queryErr, user) => { if (queryErr) { console.log(queryErr); return false; } return user; }); } 当我在signUp中调用它 signup(req, res) { if (!req.body.username || !req.body.password || !req.body.email) { return res.status(400).json({ success: false, message: 'Bad Request' }); } if (!Users.findByName(req.body.username)) { return res.status(409).json({ success: false, message: […]

我想在Node.js中更新表单数据,但是每当我运行这个代码,它将所有的值设置为null

app.put('/edit/:id', function(req, res) { //new actor data var actor = { 'name': req.body.name, 'dob': req.body.dob, 'photo': req.file, 'bio' : req.body.bio }; //updating actor Actor.findOneAndUpdate({ _id:req.params.id }, { $set: actor }, { new: true }, function(err, data) { if (err) { res.send({ status: false, error: err }); } else { res.send({ status: true, data: data }); […]

MongoDB:如何通过连接获取数据

我在MongoDB中有两个模式 var userSchema = new Schema({ first_name: String, last_name: String, email: String }); var messageSchema = new Schema({ sender: {type: Schema.Types.ObjectId, ref: 'users' }, receiver: {type: Schema.Types.ObjectId, ref: 'users' }, message: String, sentOn: Date, }); var Users = mongoose.model('users', userSchema); var Messages = mongoose.model('messages', messageSchema); 我如何查询从“用户”集合中获取所有用户,并从“消息”集合中获取最后一条消息(单个)?

mongoose版本错误:连接到mongdb

我只是不能正确连接mongdb ,我已经尝试了很多答案来解决它。 我使用了最新版本的mongoose和代码如下: mongoose.connect('mongodb://localhost/photo_app', {useMongoClient: true}); 添加useMongoClient选项没有警告和错误,但也没有反应。 我试图卸载它,并安装4.10.8版本没有警告,但它再次报告以下代码。 MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 那么对于我的问题还有其他的结论吗? 谢谢!

是否有可能在MongoDB中“join”文档并删除“父”文档,但仍保留“子”文档?

我正在用MEAN堆栈构build一个CRUD应用程序。 我有一个patients模式,我需要给这些患者添加一个medical history ,我想知道是否可以像使用mysql中的“连接”来使用这些文档。 如果是的话,甚至在病人被删除之后,我怎么能保留medical history ? 这是我patient模式 var mongoose = require("mongoose"); var Schema = mongoose.Schema; var paciente_schema = new Schema({ nombre: {type: String, required: true}, estado: {type: String, required: true, default: "En espera"}, fecha: {type: String, required: true}, edad: {type: String, required: true}, sexo: {type: String, required: true}, direccion: {type: String, required: true}, contacto: […]

使用mongoose有效地查询索引子数组

我试图在MongoDB中存储每个用户的卡片集合。 collections基本上是一系列拥有相关数量的自有卡。 Collection { user: …, cards: [ {cardId: 133, qty: 3}, {cardId: 22, qty: 1} ] } 我正在用node.js和[ {cardId: 133, qty: 2}, {cardId: 111, qty: 4} ]构build一个API,以[ {cardId: 133, qty: 2}, {cardId: 111, qty: 4} ]的forms接收请求。 现在,我需要在cards数组中创build卡片条目(如果它不存在),或者在数量已经存在的情况下更新数量。 我需要这样做,因为集合可能包含数以千计的卡,所以我想出了这个Schema : var OwnedCard = new Schema({ cardId: { type: String, index: true, required: true}, qty: { […]

Mongoose elemMatch返回一个空数组

我目前正在构build一个Web应用程序,并且在过去的12个小时里我发现了一个我无法解决的问题。 我正在研究一种基本的get方法,在这种方法中,我试图检索某个员工正在工作的某些工作职位(即收银员,职员)(客户端完美工作)。 WorkPosition的mongoose模型是这样的(models / work-position.js): var mongoose = require('mongoose'); var User = require('./user'); var Schema = mongoose.Schema; var schema = new Schema ({ workplace : {type: String, required: true}, type : {type: String, required: true}, status : {type: String, required: true}, color : {type: String, required: true}, employees: [{type: Schema.Types.ObjectId, ref:'User'}] }); module.exports = mongoose.model('WorkPosition', […]

使用Mongoose顺序创build文档

我们有一个模型,看起来像 const NodeSchema = new Mongoose.Schema({ title: { type: String, default: ''}, order: { type: Number, min: 0, required: true }, parentId: { type: String, ref: 'Node', default: null }, }); Mongoose.model('Node', NodeSchema); 我们使用Mongoosevalidation规则validationparentId是否指向以前添加的文档。 所有灯具都在JSON文件中,并且它们被sorting,以便在其父项之前不添加任何节点。 然后我们使用下面的代码加载灯具: const fixtures = JSON.parse(Fs.readFileSync(file), 'utf8'); model.create(fixtures, next); 我们的问题是灯具没有按照它们在文件上的顺序加载,validation失败。 除了逐个添加灯具(即使使用Async和Bluebird,也会造成非常难看的asynchronous代码),有没有办法让Mongoose按顺序添加灯具?

Mongooseselect字段从findOneAndUpdate返回

在Nodej中使用Mongoose可以使用find返回一些字段。 例如。 User.findOne({_id:'132324'}, {first_name:1, last_name:1}).exec… 但我似乎无法弄清楚如何使用findOneAndUpdate返回某些字段。 User.findOneAndUpdate({_id:'132324'}, {$set : {..bla bla}}, {first_name:1, last_name:1}).exec…. 有没有人以前做到这一点? 我无法在文档中find它。