How to import these file in mongodb - mongodb

How to import these file in mongodb, here is my json file
[{
"_id": ObjectId(),
"title": "How long running and abdominal excersises?",
"author": {
"userid": ObjectId(),
"name": "clark Loews",
},
"postdatetime": Date(),
},
{
"_id": ObjectId(),
"title": "How long will it take me to lose 15-20 pounds with running and abdominal excersises?",
"author": {
"userid": ObjectId(),
"name": "Bart Loews",
},
"postdatetime": Date(),
}
]
It give me this error:-Failed: error unmarshaling bytes on document #0: expected 1 argument to ObjectId constructor, but 0 received

Hmm... You'll need actual data to import. Having the ObjectId() and Date() functions in a json file will not work. How would the userid fields actually match anything in your database?
Note that you can skip the _id field when importing and MongoDB will create unique values for you, but the other fields in your document will need to be explicit.

Related

MongoDb query - aggregation, group, filter, max

I am trying to figure out specific mongoDb query, so far unsuccessfully.
Documents in my collections looks someting like this (contain more attributes, which are irrelevant for this query):
[{
"_id": ObjectId("596e01b6f4f7cf137cb3d096"),
"code": "A",
"name": "name1",
"sys": {
"cts": ISODate("2017-07-18T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d097"),
"code": "A",
"name": "name2",
"sys": {
"cts": ISODate("2017-07-19T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d098"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-16T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d099"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-10T12:40:22.772Z"),
}
}]
What I need is to get current versions of documents, filtered by code or name, or both. Current version means that from two(or more) documents with same code, I want pick the one which has latest sys.cts date value.
So, result of this query executed with filter name="name3" would be the 3rd document from previous list. Result of query without any filter would be 2nd and 3rd document.
I have an idea how to construct this query with changed data model but I was hoping someone could lead me right way without doing so.
Thank you

How to update a property of a sub-document in an embedded array?

Given the following document in the database, I want to update pincode of address array.
I'm using the $ positional locator in Mongodb. But this does not find the document embedded multiple levels.
"_id": ObjectId("58b91ccf3dc9021191b256ff"),
"phone": 9899565656,
"Email": "sumit#mail.com",
"Organization": "xyz",
"Name": "sumit",
"address": [{
"city": "chennai",
"pincode": 91,
"_id": ObjectId("58b91db48682ab11ede79b28"),
"choice": [{
"_id": ObjectId("58b91fa6901a74124fd70d89")
}]
}]
Using this query to update.
db.presenters.update({"Email":"sumit#mail.com","address.city":"chennai"},{$set:{"address.$.pincode.": 95 }})
You seem to have incorrect field name while updating, an extra dot at the end. Try following
db.presenters.update({"Email":"sumit#mail.com","address.city":"chennai"},
{$set:{"address.$.pincode": 95 }})

What is the element query in Mongodb to get value?

I am working on mongodb Aggregation query but i don't know how to get the array value from the JSON by joining two collection, please go through my below sample JSON.
COKE Collection
[{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
COKE Order Collection
{
"_id": "0908080808031338013101",
"drink": [{
"name": "Mirinda",
"_id": "894567894769476948"
}, {
"name": "COKE",
"_id": "857856856879694769"
}, {
"name": "PEPSI",
"_id": "785485686846684684"
}]
}
Expected OutPut:
Miranda name and _id should come of Orders collection by passing _id.
Above sample son i want to get the name value by joining the collections _id, please kindly go through my post and let me know if you have any suggestion.

MongoDB, remove nested doc in an array

I have the following structure in MongoDB and I try to remove the documents that contains specific tags. I can't seem to be able to get the $pull work.
In this example, I would like to pull the nested doc that has has tags :["BB"]
Any help will be appreciated !
{
"_id": 123,
"socialItems": {
"facebook": [{
"name": "firstFacebook",
"id": 2
}, {
"name": "secondFB",
"id": 43
}],
"instagram": [{
"name": "firstNstagram",
"id": 4
}],
"pc": [{
"name": "firstPC",
"id": 55,
"tags": [
"ab"
]
}, {
"name": "secondPC",
"id": 66,
"tags": [
"BB"
]
}]
}
}
I assume you are trying to drop the nested 'pc' doc, from the array? You also don't mention if you're using a specific driver for this, so I've assumed you're running this in the Mongo shell.
The following will remove documents from the 'pc' property, when containing the 'BB' tag.
db.collectionName.update({'socialItems.pc.tags': 'BB'}, {$pull: {'socialItems.pc': {tags: 'BB'}}})

Meteor MongoDB - cant insert _id with _id from API call

I am trying to call the TwitchAPI and insert some of the returned data into MongoDB. However, every time I get this error: Error: Meteor requires document _id fields to be non-empty strings or ObjectIDs.
The Twitch API response for a single stream/channel looks like this:
{
"streams": [
{
"_id": 11220687552,
"game": "League of Legends",
"viewers": 11661,
"created_at": "2014-09-30T01:10:36Z",
"_links": {
"self": "http://api.twitch.tv/kraken/streams/mushisgosu"
},
"preview": {
"small": "http://static-cdn.jtvnw.net/previews-ttv/live_user_mushisgosu-80x50.jpg",
"medium": "http://static-cdn.jtvnw.net/previews-ttv/live_user_mushisgosu-320x200.jpg",
"large": "http://static-cdn.jtvnw.net/previews-ttv/live_user_mushisgosu-640x400.jpg",
"template": "http://static-cdn.jtvnw.net/previews-ttv/live_user_mushisgosu-{width}x{height}.jpg"
},
"channel": {
"_links": {
"self": "https://api.twitch.tv/kraken/channels/mushisgosu",
"follows": "https://api.twitch.tv/kraken/channels/mushisgosu/follows",
"commercial": "https://api.twitch.tv/kraken/channels/mushisgosu/commercial",
"stream_key": "https://api.twitch.tv/kraken/channels/mushisgosu/stream_key",
"chat": "https://api.twitch.tv/kraken/chat/mushisgosu",
"features": "https://api.twitch.tv/kraken/channels/mushisgosu/features",
"subscriptions": "https://api.twitch.tv/kraken/channels/mushisgosu/subscriptions",
"editors": "https://api.twitch.tv/kraken/channels/mushisgosu/editors",
"videos": "https://api.twitch.tv/kraken/channels/mushisgosu/videos",
"teams": "https://api.twitch.tv/kraken/channels/mushisgosu/teams"
},
"background": null,
"banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/mushisgosu-channel_header_image-c5c08cce281b7be3-640x125.jpeg",
"display_name": "MushIsGosu",
"game": "League of Legends",
"logo": "http://static-cdn.jtvnw.net/jtv_user_pictures/mushisgosu-profile_image-b1c8bb5fd700025e-300x300.png",
"mature": false,
"status": "CLG hi im Gosu - Challenger AD - Smurfing Master!",
"partner": true,
"url": "http://www.twitch.tv/mushisgosu",
"video_banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/mushisgosu-channel_offline_image-7e3401b20cb5d739-640x360.png",
"_id": 41939266,
"name": "mushisgosu",
"created_at": "2013-03-31T21:12:14Z",
"updated_at": "2014-09-30T03:08:55Z",
"abuse_reported": null,
"delay": 60,
"followers": 318914,
"profile_banner": null,
"profile_banner_background_color": null,
"views": 25963780,
"language": "en-us"
}
}
],
"_total": 8477,
"_links": {
"self": "https://api.twitch.tv/kraken/streams?limit=1&offset=0",
"next": "https://api.twitch.tv/kraken/streams?limit=1&offset=1",
"featured": "https://api.twitch.tv/kraken/streams/featured",
"summary": "https://api.twitch.tv/kraken/streams/summary",
"followed": "https://api.twitch.tv/kraken/streams/followed"
}
}
The part of my server method that tries to insert the data
Meteor.call('getStreams', function(err, res) {
var data = res.data.streams;
console.log(data);
data.forEach(function(item) {
console.log(item._id);
Streams.insert({
_id: item._id,
title: item.channel.status,
author: item.channel.display_name,
url: item.url
});
});
});
getStreams simple defines the url to call and sets some variable. As you can see I am console logging the expected _id so I know it is returning a valid string but I am still getting the error. Currently, when I make the call I return 100 streams at a time and iterate through them to save the 4 fields above. Ideally, I would like to save each stream object as its own entry in the DB but all my attempts to do that have resulted in the same error and I also read somewhere that the version on "miniMongo" bundled with Meteor does not support inserting an array of objects in bulk...I also have read that miniMong does not support Collection.save() so sadly I think it will be more later to update the contents of each _id with the latest API call info since I cant just use .save to update and insert in the same statement.
I am not sure if it has any impact but I did try setting autoIndexId to false when creating the collection and it doesn't seem to matter:
Streams = new Meteor.Collection('streams', {autoIndexId: false});
Any insight is appreciated.
The problem is that the twitch _id is NOT a String, it appears to be a Number (I can tell by the output of your JSON : the number is not surrounded by quotes).
What I'd do is let Meteor generate its own internal Mongo IDs and store the twitch _id as a separate property instead.
Streams.insert({
twitchId: item._id,
title: item.channel.status,
author: item.channel.display_name,
url: item.url
});
You will have to retrieve the streams by twitchId instead of _id, but it's hardly a problem, right ?