Tag: mongodb

如何仅使用mongodb nodejs驱动程序从文本search中返回选定的字段

我正在使用本机mongodb nodejs驱动程序来执行文本search对我的mongodb数据库,我已经写了一个简单的代码如下,它工作正常,没有错误,但它返回所有字段,我试图找出如何返回只有我感兴趣的领域,我试图find在维基和手册的任何文件,但没有运气。 任何人都可以请帮忙? 谢谢 var dbclient = require('mongodb').MongoClient; dbclient.connect('mongodb://127.0.0.1:27017/test', function(err, db) { var search; if (err) { callback(err, null); return; } search = { text: collection, search: "a test" }; return db.command(search, function(err, o) { db.close(); return callback(err, o); }); });

正确的方法来转换javascriptdate时区

我正在使用以下格式返回时间值的API: 2013:02:27T06:39:25 请注意缺less时区的标识符。 从API文档: https://partner-api.groupon.com/ledger "Transaction timestamp of the ledger entry in the affiliate's time-zone.. The format is YYYY-MM-DDThh:mm:ss.. Example 2013:02:27T06:39:25" API响应时区显然是EST(子公司的时区)。 从这个派生UTC时区值的最好方法是存储在MongoDB数据库中。

在迭代器函数中完成所有任务的节点,asynchronous和callback

所以我在async.map集合中执行任务,迭代器函数在对asynchronous任务收集的所有数据执行任务之前执行多个asynchronous任务。 一个快速的伪示例如下所示: var accts=//array of accounts fechted from mongodb async.map(accts,function(acct,callback){ var likes=0; http.get(acct.facebook,function(err,resp){/*add fan count to likes*/}); http.get(acct.twitter,function(err,resp){/*add followers to likes*/}); //mongoose Model named artist artist.update({_id:acct._id},{fans:likes},function(err,acctsUpdate){}); } 我的困惑在于更新mongodb可能会在两个asynchronous任务完成之前发生,因此打破了我的应用程序。 我怎么去做这个?

mongodb mongoose忽略来自查询的当前用户

我正在编辑我的网站的编辑用户configuration文件页面。 当用户编辑他们的用户名并按保存,我想先检查,看看是否有其他用户首先有这个用户名。 User.findOne({'username' : newUsername }, function(err, user){ if (!user){ //no user exists with this name //code for changing current user's name to newUsername } else { //a user exists with this name //code for returning to the edit page, and displaying error message to user } }); 问题是当我离开用户名相同(并编辑一个不同的值,如电子邮件)。 当它查询用户名时,它在mongodb上拉我的文档,因此它不会改变任何东西(因为用户存在!)。 我怎样才能把一个特定的文件完全从查询中删除?

Node.js,mongo,promise – 将parameter passing给then函数

使用node.js,而不是callback,通过指定用户ID从mongo中检索用户对象。 Db访问使用require('mongodb').Db 。 调用从下面的代码的底部开始: findByIdPromise(userId) collection.findOne()需要userId作为参数,但是then()以promise作为单个参数来调用一个函数。 为了绕过这个限制,我传递了一个具有userId属性预设的对象finder ,以及一个采用集合promise的函数findUser ,并调用集合的findOne ,并将其传递给预设的userId。 这是一个体面的方法吗? UserProvider.prototype.getCollectionPromise = function () { var deferred = q.defer(); var resolve = deferred.resolve; var collectionCallback = function (error, usersCollection) { if (error) { throw new Error(error); } else { resolve(usersCollection); } }; this.db.collection(CollectionName, collectionCallback); return deferred.promise; }; UserProvider.prototype.findOnePromise = function () { var userId; […]

等待Mongoose查询完成

说我有以下代码: function MyClass(id, x, y, z) { var self = this; MyModel.findByID(id, function(err, model) { if(err) { // Do something } self.model = model }); this.x = x; this.y = y; this.z = z; } MyClass.prototype.myMethod = function() { console.log(this.model.id); } var obj = new MyClass(123, 7, 8, 9); obj.myMethod(); 如何确保this.model在myMethod设置,因为Mongoose查询是asynchronous的。 还是我用“非”NodeJS方式编程? 谢谢

Mongodb有条件查找

我发布3个数据到我的routes.js,我想要如果这3个数据不为空或空的查询应该是这样的: db.collection.find({posteddata1:req.body….,posteddate2:req.body….,posteddata3:req.body…}) 如果一个数据是空的或emty比我的查询: db.collection.find({posteddate2:req.body….,posteddata3:req.body…}) 如果所有的数据都是null或emtpty比我的查询shold be: db.collection.find({}); 我不想用if语句。 有没有简单的方法来做到这一点。

我如何检索所有属性是给定参数的子string的文档?

我有以下mongoose纲要 var People= new Schema({ firstName: String, alias: [String] }); 我正在寻找一种方法来返回其中一个aliasstring匹配或作为一个给定值的子string存在的所有文档。 例如: { firstName: Jon alias: ['foo', 'fuchs']} { firstName: Ann alias: ['bar', 'fuchsbar']} { firstName: Paul alias: ['foobar']} 如果我通过foob ,我想回到乔恩。 如果我通过foobar ,我想回到乔恩,安和保罗。 如果我通过fuchs ,我想回到Jon。 在Mongo中可以做这样的子string查询吗?

我怎样才能创build一个dynamic辅助函数的数据库查询

我有一个类别列表,我需要从我的应用程序中的许多位置的数据库中select。 到目前为止,我一直在查询的每一个使用后的callback链。 我只想在一个位置抓住它,所以如果我需要修改它,它就会干掉。 基本上: function() { var categoryList = {}; var Category = Parse.Object.extend("Category"); var categoryQuery = new Parse.Query(Category); categoryQuery.find(function(categories) { categories.forEach(function(item) { item=item.toJSON(); categoryList[item.objectId] = item.Name; }); }); return categoryList; } 但是我不确定把它放在哪里,我意识到在那里写的categoryList将是空的。 我如何创build一个帮助函数,为我提供可以在任何地方使用的结果? 我想我可以把它放在一个外部文件,并使用要求,但我试过: module.exports = {}; module.exports = function(fn) { <code> fn['categoryList'] = categoryList; return fn; }(module.exports); 而这似乎并没有工作。 我把这个要求放在app.js中,每次被包含,parsing就会说“更新失败,无法加载触发器”。 我很困难,而且比较新的节点/parsing。 谁能帮忙? 谢谢。

在mongodb for each循环查询后在SailsJS中渲染视图

我有2个集合名为“Keywords”和“Company”,我使用MongoDB聚合框架从关键字user key中的“Keywords”集合中检索相关的object._id。 当我从关键字集合中得到object._id后,我想使用object._id从公司集合中查询和编译最终的完整文档。 我在res.view()首先运行的部分停留在result []收集Company集合的所有文档之前。 我需要帮助把我的代码转换为同步方法。 请帮帮我。 以下是我所做的。 来自“关键字”集合的文档样本 { "_id": ObjectID("52ac7130bd40d00a7beb7a29"), "keyword": "Sunshine", "object": [ { "_id": ObjectID("528443ce751fc9b805d640ad"), "type": "companyName" } ] } 来自“公司”集合的doc样本 { "_id": ObjectID("528443ce751fc9b805d640ad"), "name": "Sunshine Plaza", … … … } SailsJS中的SearchController var keyWords = req.query.q, searchKeywords = keyWords.toLowerCase().replace(/[^\w\s]/gi, ' ').split(' '), //For example user key in "Sunshine Plaza" results = […]