MongoServerError: Cannot do exclusion on field date in inclusion projection - database

I was working with the MongoDB Atlas Server...
and encountered this error...
What does it mean...?
Can someone explain in simple words plz...
This was the query i was trying...
db.posts.find({}, {title: 1, date: 0})
The structure of the posts in database is as follows:
[
{
_id: ObjectId("63739044de169f6d0h2e6a3d"),
title: 'Post 2',
body: 'a news post',
category: 'News',
likes: 1,
tags: [ 'news', 'events' ],
date: 'Tue Nov 15 2022 18:53:24 GMT+0530 (India Standard Time)'
},
{
_id: ObjectId("63739271de179f5d0e31e5b2"),
title: 'Post 1',
body: 'hey there, hemant here',
category: 'random',
likes: 1,
tags: [ 'random', 'events' ],
date: 'Tue Nov 15 2022 18:41:24 GMT+0530 (India Standard Time)'
}
]
But I got an error which says...
MongoServerError: Cannot do exclusion on field date in inclusion projection
I was trying to get all the document objects excluding the date parameter and including the title parameter but got an error...

According to the documentation, first argument in find is filter and second is projection. projection allows you to specify fields to return._id is the only field which you need to explicitly exclude in the projection. For all other fields you just need to state the inclusion. You will have to follow the below format.
db.posts.find({}, {title: 1, body:1, category:1, likes:1, tags:1})

Related

Insert data to Atlas MongoDB error appearing: Insert not permitted while document contains errors

I'm trying to insert data to a collection I created in Atlas MongoDB. The data is following:
[
{ id: 1, performer: 'John Doe', genre: 'Rock', price: 25, day: 1, image: '/img/uploads/1fsd324fsdg.jpg' },
{ id: 2, performer: 'Rebekah Parker', genre: 'R&B', price: 25, day: 1, image: '/img/uploads/2f342s4fsdg.jpg' },
{ id: 3, performer: 'Maybell Haley', genre: 'Pop', price: 40, day: 1, image: '/img/uploads/hdfh42sd213.jpg' }
]
`I get the error : "Insert not permitted while document contains errors."
What am I doing wrong? Please advise.
may be quote problem ...
use double quote to key and property
"id" : 1, "performer : "John Doe" .. ~
This is reslved now, formatting was the problem.
I do the following:
Modify title to "title" and mongo compass works.
Example:
[
{ "id": "1", "performer": "John Doe", "genre": "Rock", "price": "25", "day": "1", "image": "/img/uploads/1fsd324fsdg.jpg" },
]
Reference document: https://docs.mongodb.com/compass/current/documents/insert
It's better that you use MongoDB Compass and connect to it with a connection string:
click on the connection
click on the connection using mongodb compass
then get the compass downloaded from MongoDB according to required OS
but use connection string of connection to MongoDB application
Once you connect to your Compass, you can use import data and then browse your file to use.

JSON inline Date converting

I have D3 data graphic like here: https://www.metricsgraphicsjs.org/examples.htm#lines with example code
MG.data_graphic({
title: "Singleton",
description: "Handling a solitary data point.",
data: [{'date': new Date('2015-03-05T21:00:00Z'), 'value': 12000}],
width: 600,
height: 200,
right: 40,
target: '#singleton'
});
and difference is that I store data into database. But I have problem with displaying dates. My JSON data (pulled from MySQL) looks like this:
var data1 = [
[
{
"date": "2018-04-28T13:42:55Z",
"value": 20.8,
"added": "2018-04-28T13:42:55Z"
}
],
[
{
"date": "2018-04-28T13:42:55Z",
"value": 22.8,
"added": "2018-04-28T13:42:55Z"
}
],
[
{
"date": "2018-04-28T13:42:55Z",
"value": 23.9,
"added": "2018-04-28T13:42:55Z"
}
],
[
{
"date": "2018-04-28T13:42:55Z",
"value": 21.3,
"added": "2018-04-28T13:42:55Z"
}
]
];
When I try to display that data, it does not work, shows me errors like <path> attribute d: Expected number, "MNaN,86.570057581…"
But on other hand, if I set another variable for example
var data2 = [
[
{'date': new Date('2015-03-05T21:07:00Z'), 'value': 12000},
{'date': new Date('2015-03-05T21:10:00Z'), 'value': 5500},
{'date': new Date('2015-03-05T21:15:00Z'), 'value': 1000}
],
[
{'date': new Date('2015-03-05T21:05:00Z'), 'value': 18000},
{'date': new Date('2015-03-05T21:07:00Z'), 'value': 15000},
{'date': new Date('2015-03-05T21:15:00Z'), 'value': 7000}
]
];
This second example works and I was wondering what this "new Date()" does to data. How can I "fix" first data1 array and store it in another variable with correct dates? - I did try this type of date: Date Sat Apr 28 2018 13:56:55 GMT+0000 (UTC) but that did not help, did not work. Like this:
[
{'date': 'Apr 28 2018 13:57:55 GMT+0000 (UTC)', 'value': 18000},
{'date': 'Apr 28 2018 13:58:55 GMT+0000 (UTC)', 'value': 15000},
{'date': 'Apr 28 2018 13:59:55 GMT+0000 (UTC)', 'value': 7000}
]
This did not work. Does "new Date()" converts date and time to integer or string or something else?
I just want to find a way to change data1 variable and convert its dates to proper values to be able to show them on graph.
Thanks guys!

Mongoid find_and_modify deprecated

I'm currently updating a Rails app to Mongoid 5. I've having trouble updating some code that uses a deprecated method (find_and_modify). Any help would be appreciated.
In Mongoid 4, I have this method to find and upsert:
LineItem.where({
date: Date.today,
location: "Location",
department: "Department"
}).find_and_modify({
"$set" => {
hours: 8,
updated_at: Time.current
},
"$setOnInsert" => {
account_id: ObjectId("5739f4534f4e48b2aa00091c"),
date: Date.today,
location: "Location",
department: "Department",
created_at: Time.current
}
}, upsert: true)
What is the equivalent using Mongoid 5?
Thanks.
From this changelog:
find_and_modify has been removed and replaced with 3 options: find_one_and_update,
find_one_and_delete and find_one_and_replace.

Filter unique values from a JSON

I am using angularjs for my page. I want to filter the values from the JSON object, So that no redundancy exists. But I didn't find any way to get the unique values from angular ng-repeat. Is there anyway to do that?
Ok, Here is some description about the question. I have a JSON in this format. This JSON I am getting from a service. So we cant expect how the repeated data occure.
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
}
]
I want the output JSON to be with no repeated elements, So that my output will be something like this
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
}
]
You can make use of Angular UI which has the unique filter defined.
The source can be found here.
Basically, you can then make use of the filter as follows:
<div ng-repeat="item in result | unique:'_id'">
//Body here
</div>
You can use 'unique'(aliases: uniq) filter in angular.filter module (https://github.com/a8m/angular-filter)
usage: colection | uniq: 'property'
you can filter by nested properties to : colection | uniq: 'property.nested_property'
So you can do something like that..
function MainController ($scope) {
$scope.orders = [
{ id:1, customer: { name: 'foo', id: 10 } },
{ id:2, customer: { name: 'bar', id: 20 } },
{ id:3, customer: { name: 'foo', id: 10 } },
{ id:4, customer: { name: 'bar', id: 20 } },
{ id:5, customer: { name: 'baz', id: 30 } },
];
}
HTML: We filters by customer id, i.e remove duplicate customers
<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
<td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>
result:
All customers list:
foo 10
bar 20
baz 30
var data = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
}
]
var uniqueNames = [];
var uniqueObj = [];
for(i = 0; i< data.length; i++){
if(uniqueNames.indexOf(data[i]._id) === -1){
uniqueObj.push(data[i])
uniqueNames.push(data[i]._id);
}
}
console.log('uniqueObj',uniqueObj)
http://jsfiddle.net/C97DJ/165/

Indexing nested documents in Solr

I've seen that Solr will allow you to index JSON:
http://wiki.apache.org/solr/UpdateJSON
However, none of the examples are nested. Can you index something like this and if not how is it normally handled?
{
name: 'ben',
state: 'california',
country: 'united states',
companies: [
{
name: 'google',
title: 'software engineer',
},
{
name: 'sherwin-williams',
title: 'web developer'
}
],
}
There are a couple ways to go. A json string can be stored explicitly, with serialization handled in the application layer. Elasticsearch uses this approach transparently.
For indexing, you can flatten the data using naming conventions. Mongodb uses such a syntax.
companies.name: ['google', 'sherwin-williams']
companies.title: ['software engineer', 'web developer']
Note in such a case a query like
<BooleanQuery: +companies.name:google +companies:web developer>
would match. If the position should matter, a more advanced SpanQuery would have to be used.
I had the same issue. We wanted to index in solr complicated json documents with arrays and maps ( much more complicated than the example that you posted).
At the end I modified the JsonLoader class to accept this kind of docuemnts. What it does , it flatten the json structure and allows the indexing of the fields and keeps the original json structure [company]. Finally it supports deep nesting
you can find the source code with some explanation on
http://www.solrfromscratch.com/2014/08/20/embedded-documents-in-solr/
On your example it will store/index [based on how you configure the fields] the following structure
name: 'ben',
state: 'california',
country: 'united states',
companies.0.name: 'google',
companies.0.title: 'software engineer',
companies.1.name: 'sherwin-williams',
companies.1.title: 'web developer'
companies_json:[
{
name: 'google',
title: 'software engineer',
},
{
name: 'sherwin-williams',
title: 'web developer'
}
]
M.
Nested Jsons can be indexed with the help of child documents in solr. We can make use of Block and join query parsers to query it.
Refer to this question

Resources