Javascript 如何使用Mongoose查找子文档?
我正在尝试使用mongoose在我的Javascript 如何使用Mongoose查找子文档?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在尝试使用mongoose在我的User集合中获取子文档。我在它的官方网站上关注了这个问题。上面写着: 每个文档都有一个_id。DocumentArray有一个特殊的id方法,用于通过文档的_id查找文档 var doc = parent.children.id(id); 这是我的密码: exports.editAccount = function(req, res) { var user = new User(req.user); var newAccount = new
User
集合中获取子文档。我在它的官方网站上关注了这个问题。上面写着:
每个文档都有一个_id。DocumentArray有一个特殊的id方法,用于通过文档的_id查找文档
var doc = parent.children.id(id);
这是我的密码:
exports.editAccount = function(req, res) {
var user = new User(req.user);
var newAccount = new Account(req.body);
console.log("Account:" + newAccount._id); // Gave me 53bf93d518254f880c000009
var account = user.accounts.id(newAccount._id);
console.log("Account" + account); // Never been printed
};
控制台日志(“帐户”+帐户)代码>从未打印过。我不知道发生了什么。我尝试了很多不同的方法,但是,仍然无法理解。任何帮助都将不胜感激
用户集合:
{
"__v" : 1,
"_id" : ObjectId("53bcf3e6fbf5adf10c000001"),
"accounts" : [
{
"accountId" : "123456789",
"type" : "Saving account",
"balance" : 100,
"_id" : ObjectId("53bf93d518254f880c000009")
}
]
}
我不太确定您是如何定义模式的,甚至基本上是如何定义模型实例的,但您真正需要的是:
var accountSchema = new Schema({
"accountId": String,
"type": { "type": String, "enum": ["Saving Account", "Checking Account"] },
"balance": { "type": Number, "default": 0 }
]);
var userSchema = new Schema({
"accounts": [accountSchema]
]);
var User = mongoose.model( "User", userSchema );
然后,当您想向用户添加帐户时,假设您的输入与第一个变量声明相匹配:
var input = {
"accountId": "123456789",
"type": "Savings Account",
};
User.findByIdAndUpdate(
userId,
{ "$push": { "accounts": input } },
function(err,user) {
// work with result in here
}
);
这确实绕过了验证和其他挂钩,但在与MongoDB通信时效率更高
如果确实需要验证和/或其他功能,则可以使用.find()
变量并发出.save()
方法
User.findById(userId,function(err,user) {
if (err) throw err; // or handle better
user.accounts.push( input );
user.save(function(err, user) {
// more handling
});
]);
要修改文档,您也要做同样的事情。通过最有效的MongoDB方式:
var input = {
accountId: "123456789",
amount: 100
};
User.findOneAndUpdate(
{ "_id": userId, "accounts.accountId": input.accountId },
{ "$inc": { "accounts.$.balance": input.amount } },
function(err,user) {
// handle result
}
);
或者在需要猫鼬钩子和/或验证的地方:
User.findById(userId,function(err,user) {
if (err) throw err; // or handle otherwise
user.accounts.forEach(function(account) {
if ( account.accountId === input.accountId )
account.balance += input.balance;
});
user.save(function(err,user) {
// handle things
});
);
请记住,这些东西是“数组”,您可以通过MongoDB方式或JavaScript方式处理它们。这取决于你选择在哪里“验证”你的输入
更多说明用法不正确的代码:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/child');
var accountSchema = new Schema({
"accountId": String,
"type": { "type": String },
"balance": { "type": Number, "default": 0 }
});
var userSchema = new Schema({
"accounts": [accountSchema]
});
var User = mongoose.model( "User", userSchema );
async.waterfall([
function(callback) {
User.create({},function(err,user) {
if (err) throw err;
console.log(
"Created:\n%s\n",
JSON.stringify( user, undefined, 4 )
);
callback(null,user);
});
},
function(user,callback) {
var account = user.accounts.create({
"accountId": "123456789",
"type": "Savings"
});
console.log(
"Account is:\n%s\n",
JSON.stringify( account, undefined, 4 )
);
console.log(
"User is still:\n%s\n",
JSON.stringify( user, undefined, 4 )
);
user.accounts.push( account );
console.log(
"User Changed:\n%s\n",
JSON.stringify( user, undefined, 4 )
);
User.findById(user.id,function(err,saved) {
if (err) throw err;
console.log(
"Persisted is still:\n%s\n",
saved
);
user.save(function(err,user) {
if (err) throw err;
callback(null,user,account);
});
});
},
function(user,account,callback) {
User.findById(user.id,function(err,saved) {
if (err) throw err;
console.log(
"Persisted is now:\n%s\n",
saved
);
var item = user.accounts.id(account.id);
console.log(
"Item is:\n%s\n",
item
);
callback();
});
}
],function(err) {
process.exit();
});
结果:
Created:
{
"__v": 0,
"_id": "53c08ab51083d1fe3852becc",
"accounts": []
}
Account is:
{
"accountId": "123456789",
"type": "Savings",
"_id": "53c08ab51083d1fe3852becd",
"balance": 0
}
User is still:
{
"__v": 0,
"_id": "53c08ab51083d1fe3852becc",
"accounts": []
}
User Changed:
{
"__v": 0,
"_id": "53c08ab51083d1fe3852becc",
"accounts": [
{
"accountId": "123456789",
"type": "Savings",
"_id": "53c08ab51083d1fe3852becd",
"balance": 0
}
]
}
Persisted is still:
{ _id: 53c08ab51083d1fe3852becc, __v: 0, accounts: [] }
Persisted is now:
{ _id: 53c08ab51083d1fe3852becc,
__v: 1,
accounts:
[ { accountId: '123456789',
type: 'Savings',
_id: 53c08ab51083d1fe3852becd,
balance: 0 } ] }
Item is:
{ accountId: '123456789',
type: 'Savings',
_id: 53c08ab51083d1fe3852becd,
balance: 0 }
谢谢你的回复。我认为你的方法应该很有效。然而,我很好奇为什么猫鼬官方文件中提到的
parent.children.id(id)
,不起作用。如果有效,这将是一条很好的捷径。@LVarayut您似乎仍然缺少使用概念。附加的代码列表和结果应该会澄清这一点。