Series is one document. It has an array of series inside it (in this case it has 'Revenge' and 'Raines').
Each series has a cast array with names. And I need a query to get those names.
Who know how can I get a list of all the names from both cast arrays?
My best approach was this query db.series.find( {}, { _id: 0, cast: 1 } ) where a get a cursor with the two cast json arrays.
{ series:
[
{
name: 'Revenge',
user_rating: 7.9,
duration: 44,
genres: [ ' Drama', ' Mystery', ' Thriller' ],
year_start: '2011',
year_end: '',
cast:
[ { name: 'Madeleine Stowe' },
{ name: 'Emily VanCamp' },
{ name: 'Gabriel Mann' },
{ name: 'Nick Wechsler' },
{ name: 'Henry Czerny' },
{ name: 'Joshua Bowman' },
{ name: 'Christa B. Allen' },
{ name: 'Ashley Madekwe' },
{ name: 'Connor Paolo' },
{ name: 'Barry Sloane' },
{ name: 'Margarita Levieva' } ],
seasons: [ { number: '3' }, { number: '2' }, { number: '1' } ]
},
{
name: 'Raines',
user_rating: 7.4,
duration: 45,
genres: [ ' Crime', ' Drama' ],
year_start: '2007',
year_end: '',
cast:
[ { name: 'Jeff Goldblum' },
{ name: 'Matt Craven' },
{ name: 'Nicole Sullivan' },
{ name: 'Linda Park' },
{ name: 'Dov Davidoff' },
{ name: 'Malik Yoba' },
{ name: 'Madeleine Stowe' } ],
seasons: [ { number: '1' } ]
}
]
}
I need an output like this:
I need this:
{ name: 'Madeleine Stowe' },
{ name: 'Emily VanCamp' },
{ name: 'Gabriel Mann' },
{ name: 'Nick Wechsler' },
{ name: 'Henry Czerny' },
{ name: 'Joshua Bowman' },
{ name: 'Christa B. Allen' },
{ name: 'Ashley Madekwe' },
{ name: 'Connor Paolo' },
{ name: 'Barry Sloane' },
{ name: 'Margarita Levieva' },
{ name: 'Jeff Goldblum' },
{ name: 'Matt Craven' },
{ name: 'Nicole Sullivan' },
{ name: 'Linda Park' },
{ name: 'Dov Davidoff' },
{ name: 'Malik Yoba' },
{ name: 'Madeleine Stowe' }
You can use aggregation framework for this:
db.series.aggregate( { $unwind : "$series" },
{ $unwind : "$series.cast" },
{ $group : { _id : "$_id",
cast : {$push:"$series.cast}
}
}
);
If you want to consolidate multiple actor appearances into one then replace $push with $addToSet.
Related
I have few fields in which I want to search for users like: username, fullname, about, desgination.
Apart from this some of the users are marked as { type: 'EXPERT' }
Now my query looks like this:
const docs = await this.userModel
.aggregate()
.search({
index: 'User',
count: { type: 'total' },
compound: {
should: [
{
text: {
query: searchQuery,
path: 'fullname',
fuzzy: { prefixLength: 2 },
score: { boost: { value: 5 } },
},
},
{
text: {
query: searchQuery,
path: 'username',
fuzzy: { prefixLength: 2 },
score: { boost: { value: 3 } },
},
},
{
text: {
query: searchQuery,
path: 'designation',
fuzzy: { prefixLength: 2 },
score: { boost: { value: 5 } },
},
},
{
text: {
query: searchQuery,
path: 'about',
fuzzy: { prefixLength: 2 },
score: { boost: { value: 1 } },
},
},
{
text: {
query: 'EXPERT',
path: 'type',
score: { boost: { value: 5 } },
},
},
],
},
})
Now this works fine until I search something rubbish like 'XYZ' and the { type: "EXPERT" } still comes on top.
What I want instead is to search in the fields: username, fullname, designation, about. And the searchQuery must match atleast one of the fields and if does match then apply the boost in the final results if any of the user in final results is of type 'EXPERT'
I am working on a Node.js project with MongoDb Database .
I need the schema of this json example :
I am working on a Node.js project with MongoDb Database .
I need the schema of this json example
MongoDb document :
{
"container_number": "ddd",
"container_Date": "2/2/2018",
"scannedProductArray": {
"CCR": [
{
"ScannedDate": {
"$date": "2018-03-28T20:54:57.663Z"
},
"productNumber": "4656874974",
"productType": "CCR"
},
{
"ScannedDate": {
"$date": "2018-03-28T20:55:23.698Z"
},
"productNumber": "4656874974",
"productType": "CCR"
}
],
"CCH": [
{
"ScannedDate": {
"$date": "2018-03-28T21:25:16.202Z"
},
"productNumber": "4656874974",
"productType": "CCR"
},
{
"ScannedDate": {
"$date": "2018-03-28T21:26:08.696Z"
},
"productNumber": "4656874974",
"productType": "CCR"
}
]
}
}
container_number: String,
container_Date: String,
scannedProductArray:{CCR:[ScannedDate: {
date:type:Date,default:Date.now
},
"productNumber:Number,
"productType": "String"],CCH[:[ScannedDate: {
date:type:Date,default:Date.now
},
"productNumber:Number,
"productType": "String"]}
May be this one helps you.
I'd like to define schemas like this:
const Product = {
ScannedDate: {
type: Object,
},
productNumber: {
type: String,
},
productType: {
type: String,
default: 'CCR',
}
};
const Item = {
itemName: {
type: [Product],
},
};
const Container = {
container_number: {
type: String,
},
container_Date: {
type: String
},
scannedProductArray: {
type: Object, // Item
}
};
If the CCR/CCH fields are dynamic, I can just use type: Object rather than a certain structure. And I validate these array items by myself instead of mongoose.
Using mongoose to perform the following operation on MongoDB:
model.findOneAndUpdate(
{ name: 'test' },
{
'$set': { name: 'test2' },
'$pull': { some_array: { '$or': [ { name: 2 }, { name: 3 } ] } }
},
function(err, c) { ... }
)
gives the following result seen in mongoose debug logs:
Mongoose: examples.findAndModify(
{ name: 'test' }) [] {
'$pull': { some_array: {} }, '$set': { name: 'test2' }
}
{ upsert: false, new: true }
some_array: {} - is empty, without $or logical operator. This call causes completely removal of some_array array which is an obvious bug.
Such syntax should be supported, because in mongo shell it does work properly using call:
db.examples.findAndModify(
{query:
{ name: 'test' },
update:
{
'$pull': { some_array: { '$or': [ { name: 2 }, { name: 3 } ] } },
'$set': { name: 'test2' }
}}
)
Is it some obvious bug in mongoose driver? If yes, does someone know the corresponding ticket for this issue or it should be reported?
Maybe there is something wrong with mongoose syntax?
Edit:
I've checked also call without $set part:
model.findOneAndUpdate(
{ name: 'test' },
{ '$set': { name: 'test2' },
'$pull': { some_array: { '$or': [ { name: 2, name: 3 } ] } }
},
function(err, c) {...}
);
And it also wrongly matches to:
Mongoose: examples.findOne({ name: 'test' }) { fields: undefined }
I have following documents in my collection
{
_id: ObjectId("5166fefbc482c31052000002"),
contact: [
{
home: 7735734105
},
{
office: 9583866301
}
],
user_name: "moti",
reportsTo: "bikram",
technology: [
{
name: ".net",
rating: 5
},
{
name: "JavaScript",
rating: 2
}
],
project: [
"Agile School",
"Draftmate"
],
type: "developer",
email: "motiranjan.pradhan#ajatus.co.in"
}
and
{
_id: ObjectId("5166fe90c482c31052000001"),
contact: [
{
home: 7735734103
},
{
office: 9583866901
}
],
user_name: "ganesh",
reportsTo: "bikram",
technology: [
{
name: "JavaScript",
rating: 3
},
{
name: ".net",
rating: 4
}
],
project: [
"SLBC",
"Draftmate"
],
type: "developer",
email: "ganesh.patra#ajatus.co.in"
}
Now I need to find the rating of the people who know only JavaScript.Currently if I run
db.users.find(
{
technology: {
$elemMatch: {
name: 'JavaScript'
}
}
},{user_name:1,'technology.name':1,_id:0}
).pretty()
I am getting names of all technologies(.net & JavaScript) and their corresponding ratings. I need only user names,and their respective ratings in JavaScript only. Do I need to use any aggregation techniques?
The positional operator '$' can be used to limit query results to the first matching element. To use in your query above you would change it to:
db.users.find( { technology: { $elemMatch: { name: 'JavaScript' } } },{user_name:1,'technology.$.name':1,_id:0} )
Here i m putting code of my json file..can anyone help me "How to parse this json file?".I tried a lot of tutorials.I got some hints.But in my json file i dont have array name.so i couldnt use any key value to access those objects.
[
{
id: 1,
name: "Ice Cube",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Venfield"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "38"
}
]
},
{
id: 2,
name: "Lite Shirt",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Otto"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "42"
}
]
},
{
id: 3,
name: "Holiday Tops",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Van Heusen"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "39"
}
]
}
]
If you're using SBJSON you'd just:
NSArray *myArray = [myJSONString JSONvalue];
And then loop through it as normal