2017-07-18 5 views
0

Ich habe 2 Sammlungen für das Produkt und Produkt price.In Produktsammlung gibt es ein product_id Feld mit dem Namen, der in String und demselben Feld ist in Product Sammlung mit demselben Namen wie String.How kann ich Schema dafür entscheiden, und wie bevölkern verwenden für welches Produkt ein Feld als Produktpreis dabei ist. Produktfelder sind = _id, Product_id, name; Produktpreisfelder sind = _id, Produkt_id, Preis; Derselbe Wert ist in Product_id für beide Sammlungen vorhanden. const productpriceSchema = mongoose.Schema({ Product_id: { type: mongoose.Schema.ObjectID, ref: 'Product' }, price: String });Wie befüllen Abfrage in Nodejs und Mongodb?

const productSchema = mongoose.Schema({ Product_Name: type: String, User_Object_ID :type: String, cid :type: String });

const Product = module.exports = mongoose.model('Product', productSchema);

const Productprice = module.exports = mongoose.model('Product_price', productpriceSchema);

module.exports.productwithprice = function(callback,limit){ Productprice.find({}, callback).populate('Product_id') }

Antwort

0
//Product Schema 
//you don't have to give field _id, mongodb will automatically generate it. 
const productSchema = new Schema({ 
    Product_Id: String, 
    name: String 
}); 

const Product = mongoose.model('Product', productSchema); 

//Product Price Schema 

const productPriceSchema = new Schema({ 
    Product_Id: { 
     type: Schema.ObjectId, 
     ref: 'Product' 
    }, 
    price: String 
}); 

const ProductPrice = mongoose.model('ProductPrice', productPriceSchema) 

ProductPrice.find({query}) 
.populate('Product_Id') 
.exec((err, products) => { 
    //Logic 
}); 
+0

Diese Abfrage funktioniert nicht. In meinem Produkt Kollektion ist die _id in Form von „_id“: ObjectId („595f4cb77e713872c1297941“), die ich mit Product Sammlung Product_id übereinstimmen soll: „595f4cb77e713872c1297941“. Bitte hilf mir dabei. –

+0

Können Sie Ihren Code bitte posten? – sohamdodia

+0

Ich habe meinen obigen Code eingereicht. –

0
var ProductSchema = new Schema({ 
    name: String, 
    productId: String 
}); 

var PriceSchema = new Schema({ 
    name: String, 
    productId: String 
}); 

ProductSchema.virtual('price', { 
    ref: 'Price', // The model to use 
    localField: 'productId', // Find price where `localField` 
    foreignField: 'productId', // is equal to `foreignField` 
    // If `justOne` is true, 'price' will be a single doc as opposed to 
    // an array. `justOne` is false by default. 
    justOne: true 
}); 

var Product = mongoose.model('Product ', ProductSchema); 
var Price = mongoose.model('Price ', PriceSchema); 

Product.find({}).populate('price').exec(function(error, products) { 
    /* `products.price` is available now */ 
}); 

Für Detail, überprüfen Sie bitte Populate Virtua ls Abschnitt Mongoose population.

+0

Diese Abfrage gibt einen Fehler, dass kein Standard-Engine angegeben wurde und keine Erweiterung wurde –

+0

, diesen Fehler zu versehen scheint etwas falsch über Ihre Render-Engine von Express zu sein, die nicht auf Mungo beziehen. Bitte überprüfen Sie Ihren Expresscode. – subaru710