I am trying to figure out how to get all of the items in a mongodb if they are matching what is trying to be found.
For example, if I want the name with "Steve" and I put "St" it will return "Steve"
I have tried:
Name.find({ name: /St/ })
where Name is require from my mongoose model.
When I do.find it seems to return a lot of stuff and not just the name. What am I doing wrong here?
Edit: So after doing
console.log(Name.find({ name: { $regex: '^Jacob' } }, { _id: 1 }));
I get this:
_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?
The caret ^
matches at the beginning of the text,
Name.find({ name: /^St/ })
Similar with$regex
Name.find({ name: { $regex: "^St" } })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.