Tag: mongodb

MongoDB提取子文档作为实际的文档

我有一个文件结构,如: { _id: '323423423fsdr2', title: 'hey', location: 'Someplace', draft: { title: 'hey im a draft', location: 'Someplace', } } 我如何查询mongo来获取草稿版本,但是如果它是根级别文档,则返回它: db.collection.find(somequery) 回报 { title: 'hey im a draft', location: 'Someplace' } 我正在研究汇总框架,但我不确定如何开始。 如果我可以避免在应用程序级别提取文档,这将是很酷的。

TypeError:对象不是节点Js中的函数

我是nodejs的初学者,我正在开发一个REST api来与mongodb进行交互。 我使用快速和mongoose在下面的教程中解释: https://codeforgeek.com/2015/08/restful-api-node-mongodb/ 我已经安装了mongo db,这里是我在js节点中的服务器代码: var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var router = express.Router(); var mongoOp = require("./models/mongo"); //var user = require("./Entities/User"); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({"extended" : false})); router.get("/",function(req,res){ res.json({"error" : false,"message" : "Hello World"}); }); router.route("/users") .get(function(req,res){ var response = {}; mongoOp.find({},function(err,data){ // Mongo command to fetch all […]

如何定义mongoose中提琴的模型和方法?

我是mongodb和nodejs的新手。 我创build了一个模式,如: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ContactSchema = new Schema({ name : String, email: String, number : String }); ContactSchema.methods.selectAll = function (callback){ console.log("I got list contact"); ContactModel.find({}).exec(function(err,items){ if(err) console.error(err.stack); else{ var notify = { 'message' : 'select all document successfully', 'data' : items, 'status' : 1 }; console.log(notify); callback(); }; […]

在Nodejs中将值推入Mongodb模型

您好我正在使用MEAN stack与两个相互关联的数据模型: 发表和评论 所以在我的PostSchema中 comments:{ type:mongoose.Schema.Types.ObjectId, ref:'Comment' 并在我的CommentSchema有 post:{ type:mongoose.Schema.Types.ObjectId, ref:'Post' } 在我的视图中input注释之后,我希望后端将注释保存在Comment集合中,并将注释保存在Post集合中。 在我的服务器app.js文件中: comment.save(function(err,comment){ if(err){return next(err);} var post = new Post(req.post) post.comments.push(comment) // OR req.post.comments.push(comment);etc post.save(function(err,post){ if(err){return next(err);} res.json(comment); }) }) 但是,在我使用post.comments.push或req.post.comments.push ,我在命令行上得到一个错误消息, push is not a function 。 以上代码来自在线教程[1]。 我search了networking,但无法find任何类似的使用push例子。 你能不能让我知道我是否被误导了,如果有另一种方式我应该这样做? [1] https://thinkster.io/mean-stack-tutorial#wiring-everything-up

MongoDB / Mongoose:使用geoJSON与geoNear方法获取不正确的距离

我已经定义了我的模式 var locationSchema = new mongoose.Schema({ coords: { type: [Number], index: '2dsphere' }, }); geoJSON对象定义为 var geoOptions = { spherical: true, maxDistance: 5000, distanceMultiplier : 0.001, num: 10 } loc.geoNear(point, geoOptions, function(error, results, stats){//do stuff}); 其中loc是我的模型实例,我的观点是 var point = { type: "Point", coordinates: [lng,lat] }; 我的数据库中有两个文件,包括coords "coords" : [ 18.9164127, 72.8245292 ] "coords" : [ […]

水线:使用AND查询元素的查询数组

我有一个模型{ title: 'title', tags: ['foo', 'bar'] } 。 如何查询包含标签foo和bar所有文档,而不用原生地同时查询? 我试过了 ContentEntry.find({tags: ['bar', 'foo'] }) 但是这会返回具有tags foo或bar文档,而我对这两个标签感兴趣。

与“mongoose”竞争状态

我有一个过程,触发了一些请求,反过来触发了一些webhooks。 当我收到所有的webhook时,我知道这个过程已经完成。 我的模型看起来像这样。 { name: 'name', status: 'PENDING', children: [{ name: 'child1', status: 'PENDING' }, { name: 'child2', status: 'PENDING' }] } 这个想法是因为webhooks进来,我更新subdoc到COMPLETE 。 在每次更新结束时,我检查其他人是否完整,如果是,我在父母上设置status='COMPLETE' 。 它看起来像是一个服务的调用将其标记为COMPLETE在另一个调用确定它仍然是PENDING之后,但在第二个调用保存之前。 当第二个保存时,它会用父母上的PENDING覆盖COMPLETE 。 以下是我的模式中的代码: methods: { doUpdate: function(data) { var child = this.children.id(data.childId); child.status = 'COMPLETE'; if (_.every(this.children.status, 'COMPLETE')) this.status = 'COMPLETE'; return this.save(); } }

为我的节点应用程序select正确的数据库

我是新的节点js&我需要build议为我的节点应用程序select数据库。 比方说,我的名单上有几个游戏(以千计)。 数组应该看起来像这样: { "Football": { "John": [{ "age": "1", "weight": "2", "height": "3", }], "Smith": [{ "age": "11", "weight": "22", "height": "33", }], … }, "Golf": { "Ricky": [{ "age": "4", "weight": "5", "height": "6", }], "Jonathan": [{ "age": "44", "weight": "55", "height": "66", }], … }, … /* Thousand more to go */ […]

如何在mongodb中获得特定月份的明智logging?

这是我的模式 empname: {type: String}, soldby: {type: String}, expenseprice:{type:Number}, expensedesc:{type:String}, expensetype:{type:String}, createdat: {type: Date, default: Date.now} 我试过这个查询 db.expenses.aggregate([ { $project: { _id: 1, year: { $year: "$createdat" }, month: { $month: "$createdat" }, day: { $dayOfMonth: "$createdat" }, expenseprice: 1 } }, { $group: { _id: { year: "$year", month: "$month", day: "$day" }, sum: { […]

Mongoose save()实际上并不保存

我试图find我的logging,更新它,并保存它。 我可以看到更新的数据,它仍然可以在save()的callback中显示更新的数据。 但是,当我去到数据库,它实际上没有更新: Skills.findOne({ skillsCat: req.body.skillsCat }, (err, gets)=> { if (err) { res.send(err) return } if (gets && gets.skillName.indexOf(req.body.skillName) !== -1) { // Here I update my data gets.percent[gets.skillName.indexOf(req.body.skillName)] = req.body.percent Console.log(gets); // Here I can see the data is updated return gets.save((err, updated)=> { Console.log(updated); // Here I can see the data is […]