使用MongoDB $in子句时,返回文档的顺序是否始终对应于数组参数的顺序?
mapreduce mongoose mongodb mongodb-query aggregation-framework
假设我有一个名为的模型User.我有一个带有对象ID的数组.
我想获得与我拥有的ID数组"相交"的所有用户记录.
User.find({ records with IDS IN [3225, 623423, 6645345] }, function....
Run Code Online (Sandbox Code Playgroud) 我想要实现以下目标:
选择我拥有的所有记录,其中所有权是我创建的对象或我管理的用户创建的对象,其中用户管理可以是管理用户的用户层次结构
所有权显然是直截了当的,可以通过与所有者对应的简单ID来处理.用户管理的层次结构让我有点难以在不通过大量ID列表的情况下执行(显然,您可以找到每个被管理的用户并使用IN子句或类似内容列出由这些用户创建的每个对象).
理想情况下,这一切都发生在单个查询中,因此可能发生正常的分页和条件.
我当时认为可能有一些数学要做到这一点 - 拥有可以某种方式进行哈希处理的ID,以确定它们是否归命令链中的任何人所有.
对这类事情的任何引用?
我错过了一些明显的东西吗
使用MongoDB如果有所作为,但很高兴考虑其他数据库的灵感.
更新: 创建了一个包含1,000,000条记录的MongoDB集合,以获得有关查询上IN子句的可管理数量参数的确切内容的一些可靠数据.当我得到一些具体信息时会报告.
分析:
使用ruby-mongo-driver和ruby基准lib.
MongoDB Collection包含1039944条记录
记录定义为:
{
first_name: String,
last_name: String,
email: String,
phone: String,
company: String,
owner: BSON::ObjectId
}
Run Code Online (Sandbox Code Playgroud)
随机生成所有字段的值.
Owner字段有一个索引.
使用以下条件运行查询:
conditions = {"owner" => { "$in" => id_list }}
opts = {skip: rand, limit: 100}
Run Code Online (Sandbox Code Playgroud)
结果:
# 10201 ids
# user system total real
# 0: 0.240000 0.000000 0.240000 ( 0.265148)
# 1: 0.240000 0.010000 0.250000 ( 0.265757)
# 2: 0.240000 0.000000 0.240000 ( 0.267149)
# …Run Code Online (Sandbox Code Playgroud) 在我的MEAN应用程序(Angular2)中,我想在删除对象本身时删除所有引用的对象。我正在将Mongoose与可删除的中间件一起使用。所以我的question.js文件看起来像这样:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Answer = require('../models/answer');
var QuestionSchema = new Schema({
content: {type: String, required: true},
questionTxt: {type: String, required: true},
position: {type: Number, min: 0, required: true},
answers: [{type: Schema.Types.ObjectId, ref: "Answer"}],
followUpQuestions: [{type: Schema.Types.ObjectId, ref: "Question"}],
additionalInfoText: {type: String},
lastChangedBy: {type: Schema.Types.ObjectId, ref: 'User'},
lastChanged: {type: Date},
isRoot: {type: Boolean}
});
/**********************************************
* Deletes all answers and questions referenced by this question
***********************************************/
schema.post('remove', function(doc) {
var deletedQuestion = doc;
//code …Run Code Online (Sandbox Code Playgroud) Select * from table_name where sport_type LIKE ('Cricket','Football');
Run Code Online (Sandbox Code Playgroud)
sport_type我正在尝试使用以下方法查找板球和足球的文档find
table.find({ sport_type : "Cricket" })
Run Code Online (Sandbox Code Playgroud)
谁能帮我这样做