我是NodeJS,MongoDB和Mongoose的新手。
当我在MongoDB客户端中运行它时,这是有效的(参见屏幕截图)。
问题:我需要做些什么才能使更新在服务器端代码中运行?
db.getCollection('vouchers').findOneAndUpdate(
{"lease":"71610b3209bb37a59c90b20c36d3bb34"},
{ $set: {'value':93} },
{ returnNewDocument: true }
)
但是,当我使用Mongoose从我的NodeJS服务器运行完全相同的代码时,它不会执行更新。
var result = Voucher.findOneAndUpdate(
{"lease":"71610b3209bb37a59c90b20c36d3bb34"},
{
$set: {'value':92}
},
{
returnNewDocument: true
}
);
结果对象如下所示:
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts: { bufferCommands: true, capped: false },
name: 'vouchers',
collectionName: 'vouchers',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'ellispark_db',
options: [Object],
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
db: [Object] },
queue: [],
buffer: false,
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: {}, _posts: {} },
base:
Mongoose {
connections: [Object],
models: [Object],
modelSchemas: [Object],
options: [Object],
plugins: [Object] },
modelName: 'Voucher',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'ellispark_db',
options: [Object],
otherDbs: [],
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: false,
db: [Object] },
discriminators: undefined,
'$appliedHooks': true,
_events: { init: [Function], save: [Function] },
_eventsCount: 2,
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
query: {},
childSchemas: [],
plugins: [Object],
s: [Object],
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'vouchers',
collectionName: 'vouchers',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
insertMany: [Function] },
schema:
Schema {
obj:
{ msisdn: [Object],
name: [Object],
lease: [Object],
description: [Object],
bcodeRef: [Object],
smsRef: [Object],
subscriberRef: [Object],
value: [Object] },
paths:
{ msisdn: [Object],
name: [Object],
lease: [Object],
description: [Object],
bcodeRef: [Object],
smsRef: [Object],
subscriberRef: [Object],
value: [Object],
_id: [Object],
__v: [Object] },
aliases: {},
subpaths: {},
virtuals: { id: [Object] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
_indexes: [],
methods: {},
statics: {},
tree:
{ msisdn: [Object],
name: [Object],
lease: [Object],
description: [Object],
bcodeRef: [Object],
smsRef: [Object],
subscriberRef: [Object],
value: [Object],
_id: [Object],
__v: [Function: Number],
id: [Object] },
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object] ],
s: { hooks: [Object], kareemHooks: [Object] },
options:
{ retainKeyOrder: false,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true },
op: 'findOneAndUpdate',
options: { retainKeyOrder: false, returnNewDocument: true },
_conditions: { lease: '71610b3209bb37a59c90b20c36d3bb34' },
_fields: undefined,
_update: { '$set': { value: 92 } },
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: [Object],
opts: [Object],
name: 'vouchers',
collectionName: 'vouchers',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
collectionName: 'vouchers' },
_traceFunction: undefined,
_count: [Function],
_execUpdate: [Function],
_find: [Function],
_findOne: [Function],
_findOneAndRemove: [Function],
_findOneAndUpdate: [Function],
_replaceOne: [Function],
_updateMany: [Function],
_updateOne: [Function] }
答案 0 :(得分:1)
findOneAndUpdate
是一个异步函数。你需要拨回电话。在此回调中,您将收到结果。
Voucher.findOneAndUpdate(
{"lease":"71610b3209bb37a59c90b20c36d3bb34"},
{
$set: {'value':92}
},
{
returnNewDocument: true
}
, function( error, result){
// In this moment, you recive a result object or error
// ... Your code when have result ... //
});
mongoose网站documentation for findOneAndUpdate
中的更多信息答案 1 :(得分:0)
return new Promise((resolve, reject) => {
collection.findOneAndUpdate({ id: id }, { $set: product }, {
returnNewDocument: true
}).then(data => {
console.log("Data", data);
resolve(data);
}, err => {
console.log("Error Update in Index1", err);
reject("Update User Failed");
});
});