Javascript 按mongoose中的_id值范围选择文档
我正在创建一个应用程序,允许用户从主表创建子表,在主表中他/她可以指定要包含在子表中的行范围 这是我的子表模型:Javascript 按mongoose中的_id值范围选择文档,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在创建一个应用程序,允许用户从主表创建子表,在主表中他/她可以指定要包含在子表中的行范围 这是我的子表模型: // Imports const mongoose = require('mongoose'); const timestamps = require('mongoose-timestamp'); // Setup const Schema = mongoose.Schema; const ViewSchema = new Schema({ name: { type:
// Imports
const mongoose = require('mongoose');
const timestamps = require('mongoose-timestamp');
// Setup
const Schema = mongoose.Schema;
const ViewSchema = new Schema({
name: {
type: String,
required: true
},
table: {
type: mongoose.Types.ObjectId,
required: true,
ref: 'Table'
},
range: [
[
{
start: { type: mongoose.Types.ObjectId, ref: 'Row' },
end: { type: mongoose.Types.ObjectId, ref: 'Row' }
}
]
],
columns: {
type: Object,
required: true
}
});
ViewSchema.plugin(timestamps);
module.exports = mongoose.model('View', ViewSchema);
我当前存储范围的起始行和结束行
现在我想知道是否有一种方法可以只选择在起始行之后和结束行之前插入的文档,这样我就不必提取1000行并在客户端过滤它们
如果这是不可能的,那么我想知道其他的解决方案
非常感谢 一个非常简单的解决方案是使用
$gt
和$lt
运算符,如下所示:
RowModel.find({ _id: { $gt: ObjectIdOfTheStart, $lt: ObjectIdOfTheEnd } });
因此,这将返回ObjectId大于起始行且小于结束行的所有行
更多关于和
正如@the_mahasagar提到的,您必须使用开始日期和结束日期,查询ObjectId也会使用相同的方法,正如Steve Ridout所提到的
警告一句,我不建议做这样的事情。您应该使用其他类型的标识符来查询这些行。您不应该通过_id而是通过createdDate来执行此操作。“开始”和“结束”可以是您想要的任何开始和结束文档的创建日期。您可以从开始日期和结束日期范围检索数据。