小编Joh*_*yHK的帖子

TypeError:'undefined'在使用mongoDB排序运算符时不是函数

我尝试使用最新日期获取文档,但是我收到此错误:TypeError: 'undefined' is not a function.

我的代码:

console.log(Games.find({authorId: authorId}).sort( { releaseDate: -1 } ).limit(1)); // error
console.log(Games.find({authorId: authorId}).fetch()); // expected outpout
Run Code Online (Sandbox Code Playgroud)

(该值releaseDate存在且ISODate格式正确.)任何帮助将不胜感激.

编辑:我正在使用Meteor 0.9.2.1.

javascript mongodb meteor

1
推荐指数
1
解决办法
1374
查看次数

在保存后中间件中使用静态方法

我有一个静态方法来执行 find() 并在应用程序启动时将营销活动数据添加到 Redis。

CampaignSchema.statics.activeCampaignsToRedis = function () {
    this
        .find()
        .where('active').equals(true)
  ...
};
Run Code Online (Sandbox Code Playgroud)

我想添加一个保存后挂钩,每当添加或修改新的营销活动时,该挂钩都会重新运行静态方法来更新 Redis 中的数据。

CampaignSchema.post('save', function (next) {
  // call CampaignSchema.statics.activeCampaignsToRedis();
});
Run Code Online (Sandbox Code Playgroud)

static-methods mongoose node.js

1
推荐指数
1
解决办法
910
查看次数

通俗地说,什么是 MongoDB 驱动程序?

我一直在搜索 MongoDB 文档、Google、Stackoverflow 和 YouTube...但我似乎仍然无法理解驱动程序在 MongoDB 中的用途

我确实知道不同的编程语言可以有一种或多种不同的驱动程序 - 但为什么我需要一个?

driver mongodb

1
推荐指数
1
解决办法
228
查看次数

Mongodb 3.0"写入不是空列表"错误

有没有人已经尝试过以下错误消息?

state应该是:write不是空列表

当我试图在我的dev mongodb中写文档时发现

mongodb

1
推荐指数
1
解决办法
3990
查看次数

MongoDB 在对象数组中搜索字符串

我在 MongoDB 中有一个结构,数组中包含不同数量的项目,称为“items”。为了进行搜索,我使用以下命令,该命令首先将内容转换为字符串,就像在 this.items 中一样,根据对象的不同,有不同的结构:

db.getCollection('docs').find.('JSON.stringify(this.items[0].value).toLowerCase().indexOf("text")!=-1')
Run Code Online (Sandbox Code Playgroud)

我的问题是,由于我不知道每个文档的项目数量,我必须使用通配符作为 this.items[*].value,但它不起作用。

有谁知道任何解决方案,或者对此有其他想法?

javascript php mongodb

1
推荐指数
1
解决办法
6666
查看次数

Update a field only if it exists in MongoDB

I have a document in collection testcol in MongoDB like this :

{
_id:111,
"xxx":"aaa",
"yyy":"bbb"
}
Run Code Online (Sandbox Code Playgroud)

I want to update the field yyy in the document, if the field is found then update it and if the field is not found then don't do anything.

When I am running my mongo db update command like db.testcol.update({_id:0},{$set:{"zzz":"ccc"}},{upsert: false}). Intentionally I gave the field as "zzz" in the update command so that it shouldn't do anything. But when I run …

upsert mongodb mongodb-query

1
推荐指数
1
解决办法
8502
查看次数

node.js 中的 $elemMatch 投影

dbo.collection("users")
            .findOne({email: emailGiven, "friends.email": element.email},
                     { friends: { $elemMatch: { email: element.email } } },
                     function(errT, resultT) {})
Run Code Online (Sandbox Code Playgroud)

我在 node.js 中有上面的查询,但由于某种原因,查询的 elemMatch 部分在 node.js 上不起作用,但是当我在 mongo 终端中执行相同的查询时,它可以工作,所以我想也许 node.js 没有支持 $elemMatch?如果是这种情况,有人能告诉我 node.js 中这个查询的等价物是什么吗?

来自我的数据库的示例数据:

/* 4 */
{
    "_id" : ObjectId("5ad20cef8248544860ce3dc1"),
    "username" : "test",
    "email": "",
    "fullName" : "",
    "friends" : [{email: "",
                 status :""}],
    "alarms": [ {"id":111,
        "title": "TITLE",
        "location": "",
        "startTime": "10-10-1996 10:18:00",
        "endTime": "10-10-1996 10:18:00" }, {},{}

    ],
     "pnumber" : ""
}
Run Code Online (Sandbox Code Playgroud)

mongodb node.js

1
推荐指数
1
解决办法
1607
查看次数

使用Mongoose从mongoDB中选择,其中ObjectId与特定ID不相等

请问如何使用模型ID与给定ID不匹配的mongoose从MongoDB中检索记录.模型设置正确.我不是Node.js,mongoDB,Mongoose的新手.

我尝试使用$ ne:

var ID = ....

User.find({id: {$ne: ID}},
function(error, users) {
  console.log(users);
  callback(error, count);
});
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用RegExp:

var regex = new RegExp('^((?!' + ID + ').)*$', "i");
User.find({id: regex},
function(error, users) {
  console.log(users);
  callback(error, users);
});
Run Code Online (Sandbox Code Playgroud)

regex mongoose mongodb node.js objectid

0
推荐指数
1
解决办法
5242
查看次数

如何动态排序,使用猫鼬更改字段和方向

exports.findall = function(req, res) {  
    var limit1 = req.params.limit;
    var stype=req.params.sorttype;
    var sdir=req.params.sortdirection;
    console.log(stype);
    console.log(sdir);
    test.find().sort({ stype : sdir }).select('_id').select('Name').skip(limit1).limit(100).exec(function(err, results) {
    res.send(results);
    });
};
Run Code Online (Sandbox Code Playgroud)

猫鼬排序动态地改变了方向和方向,我不得不使用以上代码进行排序。但是代码不起作用,以上代码无法动态更改字段,因此响应不正确,请提供一些解决方案来解决此问题。
http:// localhost:3000 / api / test / 0 / Name / 1 上面的链接我必须动态更改名称和方向,我的响应不是在“名称”字段上排序。

mongoose mongodb node.js

0
推荐指数
1
解决办法
1385
查看次数