I have some data like this :
db.movies.insert([
{
title : "Fight Club",
writer : "Chuck Palahniuk",
year : 1999,
actors : [
"Brad Pitt",
"Edward Norton",
]
},
{
title : "Pulp Fiction",
writer : "Quentin Tarantino",
year : 1994,
actors : [
"John Travolta",
"Uma Thurman",
]
},
{
title : "Inglorious Basterds",
writer : "Quentin Tarantino",
year : 2009,
actors : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth",
]
},
{
title : "The Hobbit: An Unexpected Journey",
writer : "J.R.R. Tolkein",
year : 2012,
franchise : "The Hobbit",
},
{
title : "The Hobbit: The Desolation of Smaug",
writer : "J.R.R. Tolkein",
year : 2013,
franchise : "The Hobbit",
},
{
title : "The Hobbit: The Battle of the Five Armies",
writer : "J.R.R. Tolkein",
year : 2012,
franchise : "The Hobbit",
synopsis : "Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.",
},
{
title : "Pee Wee Herman's Big Adventure"
},
{
title : "Avatar"
}
])
I need to get all movies released before the year 2000 or after 2010, So I'm writing this query:
db.movies.find( {$or: [{"year" : {$gt:2010,$lt:2000}}]})
But I'm not getting any output. Please suggest.
To insert multiple data use db.movies.insertMany([]) option.
to achieve this you can try it like this
db.movies.find({ $or: [{ "year": { $gt: 2010 } }, { "year": { $gt: 2000 } }] })
you can check this for proper documentation here
https://docs.mongodb.com/manual/reference/operator/query/or/
Related
I'm new to MongoDB and was looking through the docs a few nights ago and saw something that I haven't been able to locate since...
There was an option, I believe it was related to $text search, to treat an array of strings as if they were the same word. The syntax looked something like this:
["cheez", "cheese"],
["donut", "doughnut"],
["chips", "fries", "crisps"],
So a search for "chips" would return all documents indexed with "fries" or "crisps" even if they did not also have "chips".
Please tell me I wasn't dreaming!
YOU ARE NOT DREAMING
mongodb fuzzy text search
The following query searches the title field for the phrase naw yark. It uses the fuzzy default options where:
maxEdits allows up to two character variation of each term in the
given phrase to match the query to a document.
maxExpansions considers up to fifty similar terms for each term in
naw yark to find matches.
prefixLength is disabled.
The query also includes a $limit stage to limit the output to 10 results and a $project stage to:
Exclude all fields except title
Add a field named score
db.movies.aggregate([
{
$search: {
"text": {
"path": "title",
"query": "naw yark",
"fuzzy": {}
}
}
},
{
$limit: 10
},
{
$project: {
"_id": 0,
"title": 1,
score: { $meta: "searchScore" }
}
}
])
The above query returns the following results:
{ "title" : "New York, New York", "score" : 4.392756462097168 }
{ "title" : "New York", "score" : 4.050914287567139 }
{ "title" : "New York Stories", "score" : 3.4838104248046875 }
{ "title" : "New York Minute", "score" : 3.4838104248046875 }
{ "title" : "Synecdoche, New York", "score" : 3.4838104248046875 }
{ "title" : "New York Doll", "score" : 3.4838104248046875 }
{ "title" : "Little New York", "score" : 3.4838104248046875 }
{ "title" : "Escape from New York", "score" : 3.0559897422790527 }
{ "title" : "King of New York", "score" : 3.0559897422790527 }
{ "title" : "Naked in New York", "score" : 3.0559897422790527 }
also synonyms:
mongodb synonyms text search
We have author collection which contains author information for all the authors. We created text index using following
db.getCollection('contributors').createIndex(
{
display_name:"text",
first_name: "text",
last_name: "text"
},
{
weights: {
display_name: 10,
first_name: 5,
last_name:5
},
name: "Contributor_FTS_Index"
}
)
Here is sample data we have
{
"_id" : ObjectId("5eac8232eb5aca201f104bfb"),
"firebrand_id" : 54529588,
"agents" : null,
"created" : ISODate("2020-05-01T20:10:26.762Z"),
"display_name" : "Grace Octavia",
"email" : null,
"estates" : null,
"first_name" : "Grace",
"item_type" : "Contributor",
"last_name" : "Octavia",
"phone" : null,
"role" : 1,
"short_bio" : "GRACE OCTAVIA is the author of unforgettable novels that deal with the trials and tribulations of love, friendship, and what it means to be true to yourself. Her second novel, His First Wife, graced the Essence® bestseller list and also won the Best African-American Fiction Award from RT Book Reviews. A native of Westbury, NY, she now resides in Atlanta, GA, where there is never any shortage of material on heartache and scandal. Grace earned a doctorate in English, Creative Writing at Georgia State University in Atlanta and currently teaches at Spelman College. Visit her online at GraceOctavia.net or follow her on Twitter #GraceOctavia2.",
"slug" : "grace-octavia",
"updated" : ISODate("2020-08-05T10:10:27.691Z"),
"deleted" : false
}
{
"_id" : ObjectId("5ada44aa2ad4b3e3d0ae3daf"),
"item_type" : "Contributor",
"role" : 1,
"short_bio" : "",
"firebrand_id" : 41529135,
"display_name" : "Grace Octavia",
"first_name" : "Grace",
"last_name" : "Octavia",
"slug" : "grace-octavia",
"updated" : ISODate("2020-09-22T16:19:57.319Z"),
"agents" : null,
"estates" : null,
"deleted" : false,
"email" : null,
"phone" : null
}
{
"_id" : ObjectId("58e6ee27afbe421347a11834"),
"item_type" : "Contributor",
"role" : 1,
"short_bio" : "Octavia E. Butler (1947–2006) was a bestselling and award-winning author, considered one of the best science fiction writers of her generation. She received both the Hugo and Nebula awards, and in 1995 became the first author of science fiction to receive a MacArthur Fellowship. She was also awarded the prestigious PEN Lifetime Achievement Award in 2000. Her first novel, <i>Patternmaster</i> (1976), was praised both for its imaginative vision and for Butler’s powerful prose, and spawned four prequels, beginning with <i>Mind of My Mind</i> (1977) and finishing with <i>Clay’s Ark</i> (1984).<br /><br /> Although the Patternist series established Butler among the science fiction elite, it was <i>Kindred</i> (1979), a story of a black woman who travels back in time to the antebellum South, that brought her mainstream success. In 1985, Butler won Nebula and Hugo awards for the novella “Bloodchild,” and in 1987 she published <i>Dawn</i>, the first novel of the Xenogenesis trilogy, about a race of aliens who visit earth to save humanity from itself. <i>Fledgling</i> (2005) was Butler’s final novel. She died at her home in 2006.",
"firebrand_id" : 11532005,
"display_name" : "Octavia E. Butler",
"first_name" : "Octavia",
"last_name" : "Butler",
"slug" : "octavia-e-butler",
"updated" : ISODate("2020-09-23T04:06:18.857Z"),
"image" : "https://s3.amazonaws.com/orim-book-contributors/11532005-book-contributor.jpg",
"agents" : [
{
"name" : "Heifetz, Merrilee",
"primaryemail" : "mheifetz#writershouse.com",
"primaryphone" : "212-685-2605"
}
],
"estates" : [
{
"name" : "Estate of Octavia E. Butler",
"primaryemail" : "",
"primaryphone" : ""
}
],
"deleted" : false,
"email" : null,
"phone" : null
}
When we try to execute something like following;
db.getCollection('contributors').find({ $text: { $search: "oct" }})
it don't return any document. But if search for
db.getCollection('contributors').find({ $text: { $search: "octavia" }})
it returns all the document.
Our requirement is to give search result based on search term user entering. So it can be oc, oct, octav
Populer way to use this type of search Instead of $text so try like This,
db.contributors.find({
"$or": [
{
display_name: {
$regex: "oct",
$options: "i"
}
}
// add more fields objects same as above
]
});
You picked the wrong tool. Text search in mongo uses whole words. Read more about mongo tokenizer at https://docs.mongodb.com/manual/core/index-text/#tokenization-delimiters
The part-word index requires ngram tokenizer. It is available in full-featured text engines. E.g. based on Apache Lucene: ElasticSearch, Solr, Mongo Atlas, etc.
If your database is relatively small and weights are not essential, you can use regexp:
db.contributors.find({
"$or": [
{
displayname: {
$regex: "oct",
$options: "i"
}
},
{
first_name: {
$regex: "oct",
$options: "i"
}
},
{
last_mname: {
$regex: "oct",
$options: "i"
}
}
]
})
I have a collection of 6-7 million event records. I have another collection of ~100,000 hourly weather records spanning the same timeframe as the event records. I am using an $aggregate pipeline with $lookup to merge in relevant weather data for each event in the event collection.
THE PROBLEM: I have been running this on the full EVENT dataset for more than 8 HOURS, with no result. I have a deadline and I'm wondering if I will get a result...ever.
PLEASE HELP
Here is a sample event record:
{
"_id" : ObjectId("5dedae8111cd89b173b00910"),
"EventType" : "P",
"Jurisdiction" : "ABCD",
"Year" : 2006,
"JulianDay" : 91,
"CallReceipt" : ISODate("2006-04-01T00:00:37Z"),
"EventClosed" : ISODate("2006-04-01T00:05:25Z"),
"FinalType" : "EFGHI",
"EventWindowStart" : ISODate("2006-04-01T00:00:00Z"),
"EventWindowEnd" : ISODate("2006-04-01T01:00:00Z")
}
Here is a weather record:
{
"_id" : ObjectId("5dc3cd909fc78c0c78a336da"),
"DATE" : ISODate("2012-01-01T00:02:00Z"),
"REPORT_TYPE" : "FM-16",
"SOURCE" : 7,
"HourlyAltimeterSetting" : "30.06",
"HourlyDewPointTemperature" : "36",
"HourlyDryBulbTemperature" : "37",
"HourlyPresentWeatherType" : "BR:1 ||",
"HourlyRelativeHumidity" : 93,
"HourlySkyConditions" : "SCT:04 7 BKN:07 15 OVC:08 33",
"HourlyStationPressure" : "29.46",
"HourlyVisibility" : "5.00",
"HourlyWetBulbTemperature" : 37,
"HourlyWindDirection" : "260",
"HourlyWindSpeed" : 5,
"REM" : "MET10101/01/12 00:02:02 SPECI KROC 010502Z 26004KT 5SM BR SCT00
7 BKN015 OVC033 03/02 A3006 RMK AO2 RTX (MP)",
"REPORT_MODE" : "hourly"
}
Here is my code, typed directly into the mongo shell:
db.EVENTS.aggregate([
{
$lookup:
{
from: "WEATHER",
let : { start : "$EventWindowStart", end: "$EventWindowEnd"},
pipeline : [
{ $match :
{ $expr:
{ $and:
[
{$gte: ["$DATE", "$$start"]},
{$lte: ["$DATE", "$$end"]}
]
}
}
},
{$project: {
_id : 0,
HourlyDryBulbTemperature : 1,
HourlyPrecipitation : 1,
HourlyVisibility : 1,
WindSpeed: 1
}
}
],
as: "HourlyWeatherData"
}
},
{$out: "MERGED" }
])
On a small test subset I get the desired output. So the code works, as far as I can tell...
Sample output:
{
"_id" : ObjectId("5dedae8111cd89b173b00910"),
"EventType" : "P",
"Jurisdiction" : "ABCD",
"Year" : 2006,
"JulianDay" : 91,
"CallReceipt" : ISODate("2006-04-01T00:00:37Z"),
"EventClosed" : ISODate("2006-04-01T00:05:25Z"),
"FinalType" : "EFGHI",
"EventWindowStart" : ISODate("2006-04-01T00:00:00Z"),
"EventWindowEnd" : ISODate("2006-04-01T01:00:00Z"),
"HourlyWeatherData" : [
{
"HourlyDryBulbTemperature" : "59",
"HourlyPrecipitation" : "0.00",
"HourlyVisibility" : "10.00"
},
{
"HourlyDryBulbTemperature" : "59",
"HourlyVisibility" : "9.94"
}
]
}
PS: I do have ascending indexes on the event window fields in EVENTS, and an ascending and descending index on the DATE in WEATHER.
I am stuck here to write mongoose schema for happenings and birthdays fields. I am using mongodb/mongoose and would like to save the similar contents in events collections.
{
"_id" : ObjectId("5938fc171dfe0f225902d85d"),
"month" : 6,
"date" : 9,
"happenings" : [
{
"incident" : "Muhammad, the founder of Islam and unifier of Arabia, died.",
"year" : "632"
},
{
"incident" : "The Army of the Potomac defeats Confederate forces at Battle of Cross Keys, Virginia..",
"year" : "1862"
},
{
"incident" : "Israeli airplanes attack the USS Liberty, a surveillance ship, in the Mediterranean, killing 34 Navy crewmen..",
"year" : "1967"
},
{
"incident" : "Gemini astronaut Gene Cernan attempts to become the first man to orbit the Earth untethered to a space capsule, but is unable to when he exhausts himself fitting into his rocket pack.",
"year" : "1966"
}
],
"birthdays" : {
"actor" : {
"name" : "Josh Pence",
"yob" : 1982,
"birthplace" : "Santa Monica, CA",
"role" : "MOVIE ACTOR",
"image" : "josh_1982.png"
},
"actress" : {
"name" : "Julianna Margulies",
"yob" : 1966,
"birthplace" : "Spring Valley, NY",
"role" : "TV ACTRESS",
"image" : "julianna_1966.png"
},
"player" : {
"name" : "Julianna Margulies",
"yob" : 1987,
"birthplace" : "Ohio",
"role" : "FOOT BALL",
"image" : "julianna_1966.png"
}
}
}
The schema I tried
var schema = new Schema({
day: Number,
Month: Number,
birthdays: Schema.Types.Mixed,
happenings: [],
incident: [String],
year: [Number],
})
var event= mongoose.model('Event', schema);
How should I modify the above schema??
According to your document I think your schema could be like
var schema = new Schema({
day: Number,
Month: Number,
birthdays: Schema.Types.Mixed,
happenings: [{
incident: String,
year: String,
_id: false
}]
});
var event= mongoose.model('Event', schema);
well... I don't know anything about not relational data base and I wanna know if the following script is correct.
The data base it's developed for obtain management of users and schedules, (The schedules are related with a professor and a classroom). The classrooms have a code and they can be commented and rated by users.
db.user.insert({
//Can be 6 o 10 digits
_id : 201309,
name : {
First : "Fernando",
Last : "Sarmiento"
},
area : 2,
account : {
email : "email#google.com",
password : "Password1!",
level : 0
},
status : {
status : "Active",
Date : 17-01-2016
},
//Can be null
comment : "I like cereal",
})
-
db.schedule.insert({
_id: ObjectId(),
day : "Lunes",
hour : 13 : 00,
finalHour : 14 : 00,
teacher : {
$ref : "user",
$id : $id.user
},
classroom : {
$ref : "classroom",
$id : $id.classrom
}
})
-
db.classroom.insert({
_id : ObjectId(),
name : "Software Development",
comment : "classrom 3",
//Can take values from 0 to 5
score : $avg(userScore),
code : [
{
code : "ABCDEF123456",
user : [{
$ref : "user",
$id : $id.user
}]
}
],
post : [
{
post : "I like the classrom :)",
date : 17-01-2016,
user : [{
$ref : "user",
$id : $id.user
}]
}
],
userScore : [{
score : 4.5,
user : [{
$ref : "user",
$id : $id.user
}]
}]
})
Thanks! :3