我的数据架构有点问题。我正在使用 mongoose 构建一个带有 mongodb 和 node.js 的应用程序,这是我的架构:
var Schema = mongoose.Schema;
var misc = new Schema ({
publictransp: {type: Number},
culture: {type: Number},
traffic: {type: Number},
nature: {type: Number},
supermarket: {type: Number},
school: {type: Number},
sport: {type: Number},
nightlife: {type: Number},
mean: {type: Number}
});
var rue = new Schema ({
nomrue: {type: String},
});
var Quartiers = new Schema ({
nomquartier: {type: String},
listerue: [rue],
critere: [misc]
})
var location = new Schema ({
nomville: {type: String, required: true},
description: {type: String, required: true},
quartiers: [Quartiers],
modified: {type: Date, default: Date.now}
});
var ObjModel = mongoose.model('Obj',location);
这是应用程序代码:
//LIST PRODUCTS
app.get('/api/products', function (req, res){
return ObjModel.find(function (err, products){
if (!err) {
return res.send(products);
} else {
return console.log(err);
}
});
});
//CREATE PRODUCTS
app.post('/api/products', function(req,res){
var product;
console.log("POST: ");
console.log(req.body);
product = new ObjModel({
nomville: req.body.nomville,
description: req.body.description,
quartiers: [Quartiers],
critere: [misc],
rue: [rue]
});
product.save(function(err){
if (!err) {
return console.log("created");
} else {
return console.log(err);
}
});
return res.send(product);
});
//READ SINGLE ELEMENT
app.get('/api/products/:id', function(req,res){
return ObjModel.findById(req.params.id, function(err, product) {
if (!err) {
return res.send(product);
} else {
console.log(err);
}
return res.send(product);
});
});
//UPDATE METHOD
app.put('/api/products/:id', function(req,res){
return ObjModel.findById(req.params.id, function(err, product){
product.nomville = req.body.nomville;
product.description = req.body.description;
product.quartiers= req.body.quartiers;
return product.save(function(err){
if (!err) {
console.log("updated");
} else {
console.log(err);
}
return res.send(product);
});
});
});
//DELETE METHOD
app.delete('/api/products/:id', function(req,res){
return ObjModel.findById(req.params.id, function(err, product){
return product.remove(function(err){
if (!err) {
console.log("removed");
return res.send(' ');
} else {
console.log(err);
}
});
});
});
我将以下 Jquery 查询发送到数据库:
jQuery.post("/api/products", {
"nomville": "Strasbourg",
"description": "Ville de Strasbourg",
"quartier": [
{
"nomquartier": "Centre",
"listerue": [
{
"nomrue": "22 Novembre"
},
{
"nomrue": "Abattoir"
},
{
"nomrue": "Fonderie"
},
{
"nomrue": "Francs Bourgeois"
}
]
},
{
"nomquartier": "Cité de l'Ill",
"listerue": [
{
"nomrue": "Anguille (chemin de l)"
},
{
"nomrue": "Civelles (promenade des)"
},
{
"nomrue": "Hechner (rue)"
},
{
"nomrue": "Phario (pont)"
}
]
},
{
"nomquartier": "Krutenau",
"listerue": [
{
"nomrue": "Abreuvoir (rue de l)"
},
{
"nomrue": "Bain Finkwiller (rue du)"
},
{
"nomrue": "Fustel de Coulanges (quai)"
},
{
"nomrue": "Hôpital (place de l)"
}
]
}
],
"critere":[
{
"publictransp": 2,
"culture": 8,
"traffic": 5,
"nature": 7,
"supermarket": 3,
"school": 5,
"sport": 6,
"nightlife": 4,
"mean": 5
},
]
}, function(data, textStatus, jqXHR) {
console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR);
});
但是即使终端输出正确,我的输出也不完整,数据库显示以下输出:编辑这就是我使用您的代码 Samy 得到的结果
[
{
"nomville": "Strasbourg",
"description": "Ville de Strasbourg",
"_id": "526ee45a8d29774913000004",
"__v": 0,
"modified": "2013-10-28T22:25:30.970Z",
"quartiers": []
}
]
你可以在这里找到我原来的帖子有另一个问题:错误:key $conditionalHandlers must not start with '$' mongodb it has some extra code