forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (119 loc) · 4.99 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (119 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright 2010-2013 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ConvertGuids {
public static class Program {
private static string url;
private static string fromCollectionName;
private static GuidRepresentation fromRepresentation;
private static string toCollectionName;
private static GuidRepresentation toRepresentation;
private static bool createIndexes;
public static int Main(string[] args) {
if (!ParseArgs(args)) {
Usage();
return 1;
}
try {
ConvertCollection();
} catch (Exception ex) {
Console.WriteLine("Unhandled exception:");
Console.WriteLine(ex);
return 1;
}
return 0;
}
private static bool ParseArgs(
string[] args
) {
throw new NotImplementedException();
}
private static void Usage() {
Console.WriteLine("Converts GUIDs in a collection from one representation to another");
Console.WriteLine();
Console.WriteLine("ConvertGuids url -fromCollection name -fromRepresentation rep -toCollection name -toRepresentation rep [-createIndexes]");
Console.WriteLine("url: the URL to the database, e.g. \"mongodb://hostname/databasename\"");
Console.WriteLine("name: the name of a collection");
Console.WriteLine("rep: a GuidRepresentation (CSharpLegacy, JavaLegacy, PythonLegacy or Standard)");
}
private static void ConvertCollection() {
var database = MongoDatabase.Create(url);
var fromCollectionSettings = database.CreateCollectionSettings<BsonDocument>(fromCollectionName);
fromCollectionSettings.GuidRepresentation = fromRepresentation;
var fromCollection = database.GetCollection(fromCollectionSettings);
if (!fromCollection.Exists()) {
throw new ApplicationException("From collection does not exist.");
}
var toCollectionSettings = database.CreateCollectionSettings<BsonDocument>(toCollectionName);
toCollectionSettings.GuidRepresentation = toRepresentation;
var toCollection = database.GetCollection(toCollectionSettings);
if (toCollection.Exists()) {
throw new ApplicationException("To collection already exists.");
}
foreach (var document in fromCollection.FindAll()) {
ConvertDocument(document);
toCollection.Insert(document);
}
}
private static BsonDocument ConvertDocument(
BsonDocument document
) {
foreach (var element in document.Elements) {
element.Value = ConvertValue(element.Value);
}
return document;
}
private static BsonValue ConvertValue(
BsonValue value
) {
switch (value.BsonType) {
case BsonType.Array: return ConvertArray(value.AsBsonArray);
case BsonType.Binary: return ConvertBinaryData(value.AsBsonBinaryData);
case BsonType.Document: return ConvertDocument(value.AsBsonDocument);
case BsonType.JavaScriptWithScope: return ConvertJavaScriptWithScope(value.AsBsonJavaScriptWithScope);
default: return value;
}
}
private static BsonArray ConvertArray(
BsonArray array
) {
for (int i = 0; i < array.Count; i++) {
array[i] = ConvertValue(array[i]);
}
return array;
}
private static BsonBinaryData ConvertBinaryData(
BsonBinaryData binary
) {
if (binary.SubType == BsonBinarySubType.Uuid || binary.SubType == BsonBinarySubType.UuidLegacy) {
if (binary.GuidRepresentation != toRepresentation) {
var guid = binary.ToGuid();
return new BsonBinaryData(guid, toRepresentation);
}
}
return binary;
}
private static BsonJavaScriptWithScope ConvertJavaScriptWithScope(
BsonJavaScriptWithScope script
) {
return new BsonJavaScriptWithScope(script.Code, ConvertDocument(script.Scope));
}
}
}