Tag: mongodb

“冻结”mongoose子

我有类似的设置: B.js var schemaB = new mongoose.Schema({ x: Number, … }); module.exports = mongoose.model('B', schemaB); A.js var schemaA = new mongoose.Schema({ b: {type: mongoose.Schema.Types.ObjectId, ref: 'B'}, … }); module.exports = mongoose.model('A', schemaA); 有了这个,我的B文档就可以发生更改,而当我使用populate()检索我的A文档时,这些更改将反映在pathb的对象中。 但是,在某个特定的A文件的pathb上是否有“ 冻结 ”文件? 就像是: var id=1; A.findById(id).populate('b').exec(function(err, a) { if (err) return handleErr(err); console.log(abx); // prints 42 a.freeze('b') // fictitious freeze() fn […]

node.js,mongoose如何在多个ID中查找对象数组

我正在使用Mongoose模块的Node.js并遇到了一个问题。 我有一个Schema,这个模型看起来像这样 _id: '', foo: '', bar: '', fb: [{ id: '' }] 我如何find匹配fb [0] .id加传递一个ID数组中的集合中的所有对象? 这我已经尝试(find()方法与以下查询): {'fb[0].id': {$in: friendsIDList}} 和这个: {'fb': {$in: [{id: friendsIDList}]} } 甚至这个 {'fb': {$in: {$in: friendsIDList}}} 更清楚的是,我有一个用户对象,其中包含FB数据fb参数,但它是一个数组只包含一个对象。 现在我recive一个朋友ID列表,并希望查询所有的用户朋友。 对不起,我的英语btw。

在数组MongoDB中查找内容

我正在用Mongoose使用Mongoose 。 我有这两个模式: var feedSchema = mongoose.Schema({ users: [{ type: Schema.Types.ObjectId, ref: 'User' }] … … }); 和 var userSchema = mongoose.Schema({ email: String, … … }); 我想查找包含user._id在其users字段中的所有提要。 我想知道mongoose是否有这样的东西: Feed .find({ users : { $has : [ user._id ] } }) .exec(function(err, retData) { /* SOMETHING */ }); 我在文档中找不到像这样的东西。 谢谢!

Mongoose findandUpdate文件的多个字段

我正在尝试更新文档的多个字段: Game.findByIdAndUpdate(gameId, { $addToSet: { players: player.id }, $addToSet: { playersInfo: player }, function(err, model){…} } 但是这个查询只执行我最后一个$addToSet

未捕获的MongoError:无法识别的字段'allowDiskUsage'

我安装了2.5.5,以便我可以尝试新的“$ out”操作符来创build具有聚合结果的新集合。 我的节点适配器是mongodb@1.3.23。 我没有“allowDiskUsage”在我的代码,但我得到这个错误: Uncaught MongoError: unrecognized field 'allowDiskUsage' 我需要做什么来更新我的项目运行2.5.5?

使用Jasmine-Node进行testing时,Mongoose模型自定义函数中消失的值

我试图在我的Mongoose模型上testing我的自定义方法,但是我在testing模型中设置的值消失,并使testing失败。 testing看起来像这样: it('should get the friend id', function(){ var conversation = new Conversation({ 'users': [new ObjectId('0'), new ObjectId('1')] }); expect(conversation.getFriendId('0')).toEqual(new ObjectId('1')); }); 我的模型声明包含这个: var mongoose = require('mongoose'); var ObjectId = mongoose.Schema.Types.ObjectId; var ConversationSchema = new mongoose.Schema({ 'users': [{'type': ObjectId, 'ref': 'User'}] }); ConversationSchema.methods.getFriendId = function(userId) { return this.users; //return this.users[0] === new ObjectId(userId) // ? […]

$ addToSet与子文档的数组

我有以下更新查询: var where = 'answers.round_' + ans.round + '_' + ans.iteration Game.findByIdAndUpdate(gameId, { $addToSet: { where: ans } }, function(err, model) { if (err) throw err console.log('after game update with answer ' + JSON.stringify(model)) callback() }) 数据库结构如下所示: "_id: ObjectId("5304bc1dcf36941e3adcd3fd"), "answers" : { "round_1_1" : [], … } 问题是ans对象没有被推入"round_1_1"数组(不是我检查它不是null )。 callback模型是我想要更新的模型,只是更新不会发生。 我的问题是类似于这个 ,就像点符号不工作(即使在控制台上它输出answers.round_1_1 。

护照mongoose对象#<架构>没有方法“身份validation”,但我可以看到的方法

我遇到了一个没有道理的问题,无法弄清楚希望有人能够提供帮助。 我正在使用几个mongoose插件,现在正试图整合护照。 我已经添加了如下所示的passport-local-mongoose插件,但是我收到了该方法不存在的错误。 将模式logging到控制台时,我可以看到列出的方法,所以我不确定它为什么不存在。 代码示例: var mongoose = require('mongoose'); var timestamps = require('mongoose-timestamp'); var autoIncrement = require('mongoose-auto-increment'); var passport = require('passport'); var passportLocalMongoose = require('passport-local-mongoose'); var BasicStrategy = require('passport-http').BasicStrategy; var usersSchema = new mongoose.Schema({ firstName: String, lastName: String, email: String, organizationId: Number, description: String }); module.exports = function(app,db){ //mongoose setup options usersSchema.plugin(timestamps); usersSchema.plugin(autoIncrement.plugin, { model: […]

mongodb'sorting'不工作,如果'限制'被删除

我是新的MongoDB,我试图通过引用sorting选项: http : //mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#sorting 我的代码: MongoClient.connect('mongodb://127.0.0.1/test', function (err, db) { //connect to mongodb var collection = db.collection('qr'); var options = { "limit": 20, "sort": "CARDNO" } collection.find({}, options).toArray(function (err, data) { console.log(data.length); res.send(JSON.stringify(data)); }); console.log("successfully connected to the database"); //db.close(); }); 返回sorting结果的前20条logging。 但是当我删除“限制” MongoClient.connect('mongodb://127.0.0.1/test', function (err, db) { //connect to mongodb var collection = db.collection('qr'); […]

使用Sails.js和MongoDB创build一个search

我想在Sails.js中创build一个search页面,它将通过MongoDB进行search。 我知道如何做到这一点。 然而,我想知道是否有一个方法与水线,或任何其他选项,以解决拼写错误和替代拼写。 例如。 如果MongoDB条目是“斯普林菲尔德高中”,我怎么能说“斯普林菲尔德高中”或“spring高中”等…我假设这是可能的,它是以某种方式与水线做的,但我避难找不到任何好的文档(findLike()???)。