當我做
const currencyFound = await Currency.findOne({some_field_that_does_not_exist_in_any_currency : value});
currencyFound 永遠不會為空,邏輯上應該是什么時候?它似乎是從 Currencies 集合中提取的一些隨機檔案,即使集合中的所有檔案(包括提取的檔案)都沒有與 findOne 過濾器中指定的欄位相同的欄位(some_field_that_does_not_exist_in_any_currency)。
當我這樣做時的類似行為
const currenciesFound = await Currency.find({some_field_that_does_not_exist_in_any_currency : value});
我期望一個空串列,但我得到了集合中的所有記錄!這是怎么回事?
更新 1
如果我洗掉等待和console.log結果,這就是我得到的:
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
_executionStack: null,
mongooseCollection: Collection {
collection: Collection { s: [Object] },
Promise: [Function: Promise],
modelName: 'Currency',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
schemaUserProvidedOptions: [Object],
capped: false,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'currencies',
collectionName: 'currencies',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 1,
_closeCalled: undefined,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb://****:****/****',
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise],
db: [Db],
host: '****',
port: ****,
name: '****'
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { Currency },
schema: Schema {
obj: { name: [Object], symbol: [Object] },
paths: {
name: [SchemaString],
symbol: [SchemaString],
_id: [ObjectId],
updatedAt: [SchemaDate],
createdAt: [SchemaDate],
__v: [SchemaNumber]
},
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: { initializeTimestamps: [Function (anonymous)] },
methodOptions: {},
statics: {},
tree: {
name: [Object],
symbol: [Object],
_id: [Object],
updatedAt: [Function: Date],
createdAt: [Object],
__v: [Function: Number],
id: [VirtualType]
},
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
'$id': 2,
mapPaths: [],
s: { hooks: [Kareem] },
_userProvidedOptions: { timestamps: true },
options: {
timestamps: true,
typeKey: 'type',
id: true,
_id: true,
validateBeforeSave: true,
read: null,
shardKey: null,
discriminatorKey: '__t',
autoIndex: null,
minimize: true,
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strictQuery: true,
strict: true,
pluralization: true
},
'$timestamps': { createdAt: 'createdAt', updatedAt: 'updatedAt' },
'$globalPluginsApplied': true,
_requiredpaths: [ 'symbol', 'name' ]
},
op: 'findOne',
options: {},
_conditions: { some_field_that_does_not_exist_in_any_currency: 'GBP' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: Collection {
collection: [Collection],
Promise: [Function: Promise],
modelName: 'Currency',
_closed: false,
opts: [Object],
name: 'currencies',
collectionName: 'currencies',
conn: [NativeConnection],
queue: [],
buffer: false,
emitter: [EventEmitter]
},
collectionName: 'currencies'
},
_traceFunction: undefined,
'$useProjection': true
}
uj5u.com熱心網友回復:
Mongoose 有一個稱為strictQuery 的模式選項。如果此值設定為true,過濾器中不屬于架構的欄位將從過濾器中洗掉。它的默認值等于選項strict的值,這是true默認的。
const mySchema = new Schema({ field: Number }, { strict: true }); const MyModel = mongoose.model('Test', mySchema); // Mongoose will filter out `notInSchema: 1` because `strict: true`, > meaning this query will return // _all_ documents in the 'tests' collection MyModel.find({ notInSchema: 1 });
如果設定strictQuery為false,則不存在的欄位應保留為過濾器的一部分。
const mySchema = new Schema({ field: Number }, { strict: true, strictQuery: false // Turn off strict mode for query filters }); const MyModel = mongoose.model('Test', mySchema); // Mongoose will strip out `notInSchema: 1` because `strictQuery` is false MyModel.find({ notInSchema: 1 });
(上面的代碼中有一條注釋,指出 Mongoose將洗掉notInSchema,但我相信這可能是檔案中的錯誤,鑒于前面的示例帶有strict)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/390976.html
