i am attempting to insert an array
Here is my schema:
/**
* Created by root on 12/4/16.
*/
module.exports = function (mongoose) {
var hotPrice = mongoose.Schema({
days: String,
price: String
});
return mongoose.model('hotPrice', hotPrice);
};
And here is my route:
router.route('/createHot')
.get(function (req, res) {
var values = [
{days: 1, price: "63"},
{days: 2, price: "125"},
{days: 3, price: "185"},
{days: 4, price: "250"},
{days: 5, price: "314"},
{days: 6, price: "375"},
{days: 7, price: "425"},
{days: 8, price: "495"},
{days: 9, price: "560"},
{days: 10, price: "620"}
];
mongoose.models.hotPrice(values).insert(function (err, message) {
res.status(200);
});
});
However i get the following:
TypeError: mongoose.models.hotPrice(...).insert is not a function
Can anyone tell me how i can bulk insert this array?
You can find the answer here: http://mongoosejs.com/docs/api.html#model_Model.create
You can do as follows: (using the example of mongoose docs)
// pass an array
var values = [
{days: 1, price: "63"},
{days: 2, price: "125"},
{days: 3, price: "185"},
{days: 4, price: "250"},
{days: 5, price: "314"},
{days: 6, price: "375"},
{days: 7, price: "425"},
{days: 8, price: "495"},
{days: 9, price: "560"},
{days: 10, price: "620"}
];
YourSchemeObject.create(values, function (err, values) {
if (err) // ...
})
You also can find a similar question here:
How to save an array of objects to mongoose DB with only one call?
Hope it helps.
Related
The database contains the following data:
[
{id: 1, username: "test", amount: 2},
{id: 2, username: "example", amount: 0},
{id: 3, username: "hello", amount: 1},
{id: 4, username: "fsa", amount: 0},
{id: 5, username: "aas", amount: 10},
]
now i need to query with typeorm to retrieve the data but i need to get it sorted according to amount field and with limit only 3 users, so i should get this:
[
{id: 5, username: "aas", amount: 10},
{id: 1, username: "test", amount: 2},
{id: 3, username: "hello", amount: 1},
]
how to make that query?
Using find method following solution works. Check out TypeORM's documentation for find options for more information and other options.
userRepository.find({
take: 3,
order: {
amount: "ASC" // "DESC"
}
}
I have the following model settings:
const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
areas: [{
area: {
type: Number,
required: true,
enum: [0, 1, 2, 3, 4, 5],
}
}],
}, {
timestamps: true
});
const Item = mongoose.model('Item', itemSchema);
module.exports = Item;
When I try to save an instance in the DB:
const item = new Item({
areas: [1, 2, 3],
});
item.save();
I get the following error:
Item validation failed: areas: Cast to embedded failed for value "1" at path "areas"
CastError: Cast to embedded failed for value "1" at path "areas"
Why do I get this error? I can't find what Schema rule I broke..
I think its because you are setting the areas as an array of objects that contains area property, like:
[{area: 1}, {area: 2}, {area: 3}]
Maybe you could try set it as:
areas: [{
type: Number,
required: true,
enum: [0, 1, 2, 3, 4, 5],
}],
[ { id: 4,
category_id: 7,
user_id: 2,
title: '',
image1: '15717679702861gohz24vrk2262fov.jpg',
image2: '15717679702891gohz24vrk2262fox.jpg',
image3: '15717679702911gohz24vrk2262foz.jpg',
image4: '15717679702921gohz24vrk2262fp0.jpg',
image5: '15717679702931gohz24vrk2262fp1.jpg',
status: 2,
created_at: 2019-10-22T18:12:50.000Z,
updated_at: 2019-10-22T18:12:50.000Z,
item_id: 1,
category_field_id: 4,
value: 'Iphone',
is_title: 1,
is_description: 0 },
{ id: 3,
category_id: 7,
user_id: 2,
title: '',
image1: '15717679702861gohz24vrk2262fov.jpg',
image2: '15717679702891gohz24vrk2262fox.jpg',
image3: '15717679702911gohz24vrk2262foz.jpg',
image4: '15717679702921gohz24vrk2262fp0.jpg',
image5: '15717679702931gohz24vrk2262fp1.jpg',
status: 2,
created_at: 2019-10-22T18:12:50.000Z,
updated_at: 2019-10-22T18:12:50.000Z,
item_id: 1,
category_field_id: 3,
value: 'Other',
is_title: 0,
is_description: 0 },
{ id: 11,
category_id: 7,
user_id: 2,
title: '',
image1: '15718960965161gohz24rzk24acmit.jpg',
image2: '15718960965181gohz24rzk24acmiu.jpg',
image3: '15718960965191gohz24rzk24acmiv.jpg',
image4: '15718960965201gohz24rzk24acmiw.jpg',
image5: '15718960965221gohz24rzk24acmiy.jpg',
status: 0,
created_at: 2019-10-24T05:48:16.000Z,
updated_at: 2019-10-24T05:48:16.000Z,
item_id: 3,
category_field_id: 3,
value: 'Other',
is_title: 0,
is_description: 0 } ]
i want to group by this on item_id. but only those who have a specific length that is enter by user (number of inputs by user in search)
suppose length is 2 so the item that having only two object included in final result;
like
{
1: [{ id: 4,
category_id: 7,
user_id: 2,
title: '',
image1: '15717679702861gohz24vrk2262fov.jpg',
image2: '15717679702891gohz24vrk2262fox.jpg',
image3: '15717679702911gohz24vrk2262foz.jpg',
image4: '15717679702921gohz24vrk2262fp0.jpg',
image5: '15717679702931gohz24vrk2262fp1.jpg',
status: 2,
created_at: 2019-10-22T18:12:50.000Z,
updated_at: 2019-10-22T18:12:50.000Z,
item_id: 1,
category_field_id: 4,
value: 'Iphone',
is_title: 1,
is_description: 0 },
{ id: 3,
category_id: 7,
user_id: 2,
title: '',
image1: '15717679702861gohz24vrk2262fov.jpg',
image2: '15717679702891gohz24vrk2262fox.jpg',
image3: '15717679702911gohz24vrk2262foz.jpg',
image4: '15717679702921gohz24vrk2262fp0.jpg',
image5: '15717679702931gohz24vrk2262fp1.jpg',
status: 2,
created_at: 2019-10-22T18:12:50.000Z,
updated_at: 2019-10-22T18:12:50.000Z,
item_id: 1,
category_field_id: 3,
value: 'Other',
is_title: 0,
is_description: 0 }
]
}
By default, lodash doesn't provide the feature you need.
Instead, you can try combining _groupBy and _pickBy functions.
var groupedItems = _.groupBy(items, 'item_id');
var userSearchLength = 2;
var filteredGrouping = _.pickBy(groupedItems, function(value, key) {
return value.length == userSearchLength;
});
const userInputNumber = 2;
let grouped =_groupBy(obj,(o)=> o.item_id)
Object.keys(grouped).forEach(item_id=> {
if(grouped[item_id].length !== userInputNumber){
delete grouped[item_id];
}
})
You'll have to group first and then check and delete groups which don't have the length of array you desire.
I hope this will help you.
You can first group the data and then filter the data that you needed.
const givenLength = 2;
const groupData =_.groupBy(data,(o)=> o.item_id);
const filtered = _.filter(groupData, (o) => o.length === givenLength );
Given these models:
Products:
const productSchema = new Schema({
name: String,
description: String,
price: Number,
steps: [{
type: Schema.Types.ObjectId,
ref: 'steps'
}]
});
Steps:
const stepSchema = new Schema({
name: String,
minimum: Number,
maximum: Number,
items: [
{
name: String,
price: Number
}
]
});
Is there a better way to query an object that has N number of Products and not all products.steps selected without using .populate() (because it will make a request for each value inside the products.steps array) and a for loop to sum all prices included in productSchema and stepSchema?
Exemple of use:
On my documents I've got product collection like this:
[{
id: 1
name: Pizza,
description: "Medium (3 flavors)",
price: 10,
steps: [{
"1",
"2"
}]
},
{
id: 2
name: Pizza,
description: "small (1 flavors)",
price: 10,
steps: [{
"1",
}]
}]
And steps like:
[{
id: 1
name: "Flavor",
minimum: 0,
maximum: 1,
items: [
{
id: 1
name: "mozzarella",
price: 4
},
{
id: 2
name: "pepperoni",
price: 5
}
]
},
{
id: 2
name: "Border",
minimum: 0,
maximum: 0,
items: [
{
id: 3
name: "With normal cheese",
price: 0
},
{
id: 4
name: "With special cheese",
price: 3
}
]
}]
So is there a better way to validade an object like this in my BD with less query and without a for loop?
products: [
{
productId: 1,
price: 30,
quantity: 3,
steps: [{
stepId: 1
items: [
{
itemId: 1,
price: 4
},
{
itemId: 2,
price: 5
}
]
}]
}
]
I think below steps should be followed:
$lookup(aggregation) or populate
$project(aggregation)
The problem is that if the children are larger than 30 pieces then the latest box rises to the top and spoils the entire structure.
I took here some code examples for you.
Please help me, I can't explain problem in details because my English is very bad.
dataSource: [
{ id: 1, parentId: null, name: "Amber McKenzie"},
{ id: 2, parentId: 1, name: "Ava Field"},
{ id: 37, parentId: 2, name: "Evie Johnson"},
{ id: 3, parentId: 1, name: "Evie Johnson"},
{ id: 4, parentId: 1, name: "Evie Johnson"},
{ id: 5, parentId: 1, name: "Evie Johnson"},
{ id: 6, parentId: 1, name: "Evie Johnson"},
{ id: 7, parentId: 1, name: "Evie Johnson"},
{ id: 8, parentId: 1, name: "Evie Johnson"},
{ id: 9, parentId: 1, name: "Evie Johnson"},
{ id: 10, parentId: 1, name: "Evie Johnson"},
{ id: 11, parentId: 1, name: "Evie Johnson"},
{ id: 12, parentId: 1, name: "Evie Johnson"},
{ id: 13, parentId: 1, name: "Evie Johnson"},
{ id: 14, parentId: 1, name: "Evie Johnson"},
{ id: 15, parentId: 1, name: "Evie Johnson"},
{ id: 16, parentId: 1, name: "Evie Johnson"},
{ id: 17, parentId: 1, name: "Evie Johnson"},
{ id: 18, parentId: 1, name: "Evie Johnson"},
{ id: 19, parentId: 1, name: "Evie Johnson"},
{ id: 20, parentId: 1, name: "Evie Johnson"},
{ id: 21, parentId: 1, name: "Evie Johnson"},
{ id: 22, parentId: 1, name: "Evie Johnson"},
{ id: 23, parentId: 1, name: "Evie Johnson"},
{ id: 24, parentId: 1, name: "Evie Johnson"},
{ id: 25, parentId: 1, name: "Evie Johnson"},
{ id: 26, parentId: 1, name: "Evie Johnson"},
{ id: 27, parentId: 1, name: "Evie Johnson"},
{ id: 28, parentId: 1, name: "Evie Johnson"},
{ id: 29, parentId: 1, name: "Evie Johnson"},
{ id: 30, parentId: 1, name: "Evie Johnson"},
{ id: 31, parentId: 1, name: "Evie Johnson"},
{ id: 32, parentId: 1, name: "Evie Johnson"}
]
The default value of maxDepth option is 30. In you case you have to increase it. Here is an example:
var orgchart = new getOrgChart(document.getElementById("people"), {
primaryFields: ["name"],
layout: getOrgChart.MIXED_HIERARCHY_RIGHT_LINKS,
maxDepth: 50,