[英]How to find if a name is in a mongodb in node.js?
我試圖弄清楚如何獲取 mongodb 中的所有項目,如果它們與試圖找到的項目匹配。
例如,如果我想要帶有“Steve”的名字並輸入“St”,它將返回“Steve”
我努力了:
Name.find({ name: /St/ })
我的 mongoose model 需要名稱。
當我 do.find 時,它似乎返回了很多東西,而不僅僅是名稱。 我在這里做錯了什么?
編輯:所以在做之后
console.log(Name.find({ name: { $regex: '^Jacob' } }, { _id: 1 }));
我明白了:
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
_executionCount: 0,
mongooseCollection: NativeCollection {
collection: Collection { s: [Object] },
Promise: [Function: Promise],
_closed: false,
opts: {
autoIndex: true,
autoCreate: undefined,
schemaUserProvidedOptions: [Object],
capped: false,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'names',
collectionName: 'names',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: '',
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise],
name: 'Cluster0',
host: 'cluster0-shard-00-00.cgc8h.mongodb.net',
port: 27017,
user: 'admin',
pass: 'admin',
db: [Db]
},
queue: [],
buffer: false,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { Name },
schema: Schema {
obj: { name: [Object] },
paths: { name: [SchemaString], _id: [ObjectId], __v: [SchemaNumber] },
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
name: [Object],
_id: [Object],
__v: [Function: Number],
id: [VirtualType]
},
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
'$id': 1,
s: { hooks: [Kareem] },
_userProvidedOptions: { toJSON: [Object] },
options: {
toJSON: [Object],
typePojoToMixed: true,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strictQuery: false,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true
},
op: 'find',
options: {},
_conditions: { name: { '$regex': '^Jacob' } },
_fields: { _id: 1 },
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: NativeCollection {
collection: [Collection],
Promise: [Function: Promise],
_closed: false,
opts: [Object],
name: 'names',
collectionName: 'names',
conn: [NativeConnection],
queue: [],
buffer: false,
emitter: [EventEmitter]
},
collectionName: 'names'
},
_traceFunction: undefined,
'$useProjection': true,
_userProvidedFields: { _id: 1 }
}```
I am looking to just return the name which in this case would be Jacob. What am I doing wrong?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.