mongo db + node returns 连接字符串
mongo db + node returns the connection string
我正在开发一个抓取所有电报消息的程序,但我们发现我们需要一种简单的方法来插入和读取数据 (crud)。我选择函数式编程是因为 oop 对我来说太模仿太复杂了(如果有人有建议 请 打我,因为我不知道从哪里开始)。因为我想要 value.insert
之类的东西,但经过一番研究并询问了 cs 老师后,我发现这行不通。
查找数据的函数:
connect.then( async () => {
const dbo = client.db('telegram');
const options = {
projection: {_id : 1, name: 1, town: 1},
}
const result = await dbo.collection(collection).find(data)
console.log(result)
})
}
// how the function is called
Find('dev',{name: 'Red'})
下面的代码块是如何console.log(result);看起来像
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false,
[Symbol(topology)]: Topology {
_events: [Object: null prototype] {
topologyDescriptionChanged: [Array],
connectionPoolCreated: [Function (anonymous)],
connectionPoolClosed: [Function (anonymous)],
connectionCreated: [Function (anonymous)],
connectionReady: [Function (anonymous)],
connectionClosed: [Function (anonymous)],
connectionCheckOutStarted: [Function (anonymous)],
connectionCheckOutFailed: [Function (anonymous)],
connectionCheckedOut: [Function (anonymous)],
connectionCheckedIn: [Function (anonymous)],
connectionPoolCleared: [Function (anonymous)],
commandStarted: [Function (anonymous)],
commandSucceeded: [Function (anonymous)],
commandFailed: [Function (anonymous)],
serverOpening: [Function (anonymous)],
serverClosed: [Function (anonymous)],
serverDescriptionChanged: [Function (anonymous)],
topologyOpening: [Function (anonymous)],
topologyClosed: [Function (anonymous)],
error: [Function (anonymous)],
timeout: [Function (anonymous)],
close: [Function (anonymous)],
serverHeartbeatStarted: [Function (anonymous)],
serverHeartbeatSucceeded: [Function (anonymous)],
serverHeartbeatFailed: [Function (anonymous)]
},
_eventsCount: 25,
_maxListeners: undefined,
bson: [Object: null prototype] {
serialize: [Function: serialize],
deserialize: [Function: deserialize]
},
s: {
id: 0,
options: [Object: null prototype],
seedlist: [Array],
state: 'connected',
description: [TopologyDescription],
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
minHeartbeatFrequencyMS: 500,
servers: [Map],
sessionPool: [ServerSessionPool],
sessions: Set(0) {},
credentials: [MongoCredentials],
clusterTime: [Object],
connectionTimers: Set(0) {},
detectShardedTopology: [Function: detectShardedTopology],
detectSrvRecords: [Function: detectSrvRecords],
srvPoller: [SrvPoller]
},
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: Denque {
_head: 3,
_tail: 3,
_capacity: undefined,
_capacityMask: 3,
_list: [Array]
}
},
[Symbol(namespace)]: MongoDBNamespace { db: 'telegram', collection: 'dev' },
[Symbol(documents)]: [],
[Symbol(initialized)]: false,
[Symbol(closed)]: false,
[Symbol(killed)]: false,
[Symbol(options)]: {
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
},
fieldsAsRaw: {},
promoteValues: true,
promoteBuffers: false,
promoteLongs: true,
serializeFunctions: false,
ignoreUndefined: false,
bsonRegExp: false,
raw: false
},
[Symbol(filter)]: { name: 'Red' },
[Symbol(builtOptions)]: {
raw: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
}
}
}
预期的行为应该是 returns 所有具有值名称的文档:Red
如果这对数据库有帮助的话:
[for the minimum text] 我尝试在线查看是否有关于 returns 连接字符串
的模块的一些信息
如果您想从没有对象的模型中获取数据
你可以使用
exports.findFriend = async (userName,user)=>{
return await User.findOne({_id:user._id},{userName = 'Red');
};
如果你想从一个有对象的模型中获取数据,你可以使用
exports.findFriend = async (userName,user)=>{
return await User.findOne({_id:user._id},{friends:{$elemMatch:{userName:userName}}});
};
原来我忘了对结果使用 .toarray()
我正在开发一个抓取所有电报消息的程序,但我们发现我们需要一种简单的方法来插入和读取数据 (crud)。我选择函数式编程是因为 oop 对我来说太模仿太复杂了(如果有人有建议 请 打我,因为我不知道从哪里开始)。因为我想要 value.insert
之类的东西,但经过一番研究并询问了 cs 老师后,我发现这行不通。
查找数据的函数:
connect.then( async () => {
const dbo = client.db('telegram');
const options = {
projection: {_id : 1, name: 1, town: 1},
}
const result = await dbo.collection(collection).find(data)
console.log(result)
})
}
// how the function is called
Find('dev',{name: 'Red'})
下面的代码块是如何console.log(result);看起来像
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false,
[Symbol(topology)]: Topology {
_events: [Object: null prototype] {
topologyDescriptionChanged: [Array],
connectionPoolCreated: [Function (anonymous)],
connectionPoolClosed: [Function (anonymous)],
connectionCreated: [Function (anonymous)],
connectionReady: [Function (anonymous)],
connectionClosed: [Function (anonymous)],
connectionCheckOutStarted: [Function (anonymous)],
connectionCheckOutFailed: [Function (anonymous)],
connectionCheckedOut: [Function (anonymous)],
connectionCheckedIn: [Function (anonymous)],
connectionPoolCleared: [Function (anonymous)],
commandStarted: [Function (anonymous)],
commandSucceeded: [Function (anonymous)],
commandFailed: [Function (anonymous)],
serverOpening: [Function (anonymous)],
serverClosed: [Function (anonymous)],
serverDescriptionChanged: [Function (anonymous)],
topologyOpening: [Function (anonymous)],
topologyClosed: [Function (anonymous)],
error: [Function (anonymous)],
timeout: [Function (anonymous)],
close: [Function (anonymous)],
serverHeartbeatStarted: [Function (anonymous)],
serverHeartbeatSucceeded: [Function (anonymous)],
serverHeartbeatFailed: [Function (anonymous)]
},
_eventsCount: 25,
_maxListeners: undefined,
bson: [Object: null prototype] {
serialize: [Function: serialize],
deserialize: [Function: deserialize]
},
s: {
id: 0,
options: [Object: null prototype],
seedlist: [Array],
state: 'connected',
description: [TopologyDescription],
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
minHeartbeatFrequencyMS: 500,
servers: [Map],
sessionPool: [ServerSessionPool],
sessions: Set(0) {},
credentials: [MongoCredentials],
clusterTime: [Object],
connectionTimers: Set(0) {},
detectShardedTopology: [Function: detectShardedTopology],
detectSrvRecords: [Function: detectSrvRecords],
srvPoller: [SrvPoller]
},
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: Denque {
_head: 3,
_tail: 3,
_capacity: undefined,
_capacityMask: 3,
_list: [Array]
}
},
[Symbol(namespace)]: MongoDBNamespace { db: 'telegram', collection: 'dev' },
[Symbol(documents)]: [],
[Symbol(initialized)]: false,
[Symbol(closed)]: false,
[Symbol(killed)]: false,
[Symbol(options)]: {
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
},
fieldsAsRaw: {},
promoteValues: true,
promoteBuffers: false,
promoteLongs: true,
serializeFunctions: false,
ignoreUndefined: false,
bsonRegExp: false,
raw: false
},
[Symbol(filter)]: { name: 'Red' },
[Symbol(builtOptions)]: {
raw: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
ignoreUndefined: false,
bsonRegExp: false,
serializeFunctions: false,
fieldsAsRaw: {},
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined,
maxStalenessSeconds: undefined,
minWireVersion: undefined
}
}
}
预期的行为应该是 returns 所有具有值名称的文档:Red
如果这对数据库有帮助的话:
[for the minimum text] 我尝试在线查看是否有关于 returns 连接字符串
的模块的一些信息如果您想从没有对象的模型中获取数据 你可以使用
exports.findFriend = async (userName,user)=>{
return await User.findOne({_id:user._id},{userName = 'Red');
};
如果你想从一个有对象的模型中获取数据,你可以使用
exports.findFriend = async (userName,user)=>{
return await User.findOne({_id:user._id},{friends:{$elemMatch:{userName:userName}}});
};
原来我忘了对结果使用 .toarray()