在查询性能方面有点奇怪...我需要运行一个查询来完成文档的总计数,并且还可以返回一个可以限制和偏移的结果集.
所以,我总共有57个文档,用户希望10个文档偏移20个.
我可以想到两种方法,首先查询所有57个文档(作为数组返回),然后使用array.slice返回他们想要的文档.第二个选项是运行2个查询,第一个使用mongo的本机'count'方法,然后使用mongo的本机$ limit和$ skip聚合器运行第二个查询.
您认为哪个会更好地扩展?在一个查询中完成所有操作,还是运行两个单独的查询?
编辑:
// 1 query
var limit = 10;
var offset = 20;
Animals.find({}, function (err, animals) {
if (err) {
return next(err);
}
res.send({count: animals.length, animals: animals.slice(offset, limit + offset)});
});
// 2 queries
Animals.find({}, {limit:10, skip:20} function (err, animals) {
if (err) {
return next(err);
}
Animals.count({}, function (err, count) {
if (err) {
return next(err);
}
res.send({count: count, animals: animals});
});
});
Run Code Online (Sandbox Code Playgroud) 好吧,所以我正在构建一个基于Node.js的应用程序,我正在使用mongoose来处理我与mongodb的连接.我有一个端点是这样的:
getTestStream : function(req, res, conditions, callback) {
Activity.find()
.limit(1000)
.run(function(err, activities) {
if (err){
util.sendError(req, res, "Query Error", err);
} else if (activities) {
res.send(activities);
} else {
util.send('nope');
}
});
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,此调用需要700毫秒+才能完成.即使没有应用mongodb shell的限制,相同的调用也会在大约4ms内返回.这看起来像是一个简单的查询,那么是什么让它减慢了这么多?我猜我错过了配置中某些显而易见的东西,但我不知道.
感谢任何可以提供帮助的人.
其他信息:
mongoose@2.6.0
mongodb@2.0.4
node@0.6.9
Run Code Online (Sandbox Code Playgroud) 我想在Apollo graphql server中实现基于游标的分页.我已经准备了具有分页要求的模式.但我被困在解决方案方面.这是我的架构
const typeDefinitions = `
input CreateDeveloperInput {
# An arbitrary string value with no semantic meaning. Will be included in the
# payload verbatim. May be used to track mutations by the client.
clientMutationId: String
developer: DeveloperInput!
}
type CreateDeveloperPayload {
clientMutationId: String
developerEdge(orderBy: DevelopersOrderBy = PRIMARY_KEY_ASC): DevelopersEdge
query: Query
}
input DeleteDeveloperByIdInput {
# An arbitrary string value with no semantic meaning. Will be included in the
# payload verbatim. May be used to track mutations …Run Code Online (Sandbox Code Playgroud)我想使用官方 mongodb 包在node.js环境中实现mongodb分页。我试图在互联网上查找,但都是基于猫鼬的链接。我不想使用猫鼬。
如何使用
http://mongodb.github.io/node-mongodb-native/3.1/api/给出的官方客户端 api 实现分页
我正在使用Express、EJS和 MongoDB 开发博客应用程序(单击链接以查看GitHub 存储库)。
我为帖子制作了一个简单的寻呼机。
在帖子控制器中,我有:
exports.getPosts = async (req, res, next) => {
const posts = await Post.find({}, (err, posts) => {
const perPage = 10;
const currPage = req.query.page ? parseInt(req.query.page) : 1;
const postsCount = posts.length;
const pageCount = Math.ceil(postsCount / perPage);
const pageDecrement = currPage > 1 ? 1 : 0;
const pageIncrement = currPage < pageCount ? 1 : 0;
if (err) {
console.log("Error: ", …Run Code Online (Sandbox Code Playgroud) 我正在创建一个RESTful API,它将返回MongoDB集合中的文档.作为RESTful,我想将返回的文档数量限制为25,然后让客户端请求下一个25,然后是下一个,依此类推,直到所有文档都被读取为止.使用find()我可以在集合中获取'all'文档,并使用find().limit()我可以将它限制为25,但它总是会得到第25个.是否有任何好的代码示例显示如何记住你在find()中停止的位置,以便第二次调用find将返回集合中的下25个文档?我的代码到目前为止......
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
Transaction.find(function (err, transactions) {
if (err) {
mongoose.connection.close();
res.send('FAIL');
} else {
mongoose.connection.close();
res.send(transactions);
}
}).limit(25);
});
Run Code Online (Sandbox Code Playgroud)
TX!
我正在使用express,mongoose,node.so我已经检查了有关跳过和限制文档的查询.
Model.find({},null,{limit:2,skip:20},function(err,data){
//console.log(data)
})
Run Code Online (Sandbox Code Playgroud) node.js ×5
mongodb ×4
mongoose ×4
express ×2
javascript ×2
database ×1
ejs ×1
graphql ×1
graphql-js ×1
mean-stack ×1
pagination ×1